test: replace js code with ts code

上级 84732c81
"use strict";
//------------------------------------------------------------------------------
// Expression Bodies
// More expressive closure syntax.
// http://es6-features.org/#ExpressionBodies
//------------------------------------------------------------------------------
odds = evens.map(v => v + 1)
pairs = evens.map(v => ({ even: v, odd: v + 1 }))
nums = evens.map((v, i) => v + i)
//------------------------------------------------------------------------------
// Statement Bodies
// More expressive closure syntax.
// http://es6-features.org/#StatementBodies
//------------------------------------------------------------------------------
nums.forEach(v => {
if (v % 5 === 0)
fives.push(v)
})
//------------------------------------------------------------------------------
// Lexical this
// More intuitive handling of current object context.
// http://es6-features.org/#Lexicalthis
//------------------------------------------------------------------------------
this.nums.forEach((v) => {
if (v % 5 === 0)
this.fives.push(v)
})
\ No newline at end of file
async function f(){}
class C {
async method(){}
static async method1(){}
async static #method2(){}
async *gen(){}
async get v(){return 1};
}
async ()=>{};
async ()=>{await f();}
async ()=>{await promise;}
async ()=>{await 1;}
async function* gen(){}
\ No newline at end of file
// Check whether static members are parsed inside a class
// without explicit strict mode.
class ClassWithoutExplicitStrictMode {
static myStaticMethod() {}
}
"use strict";
//------------------------------------------------------------------------------
// Class Definition
// More intuitive, OOP-style and boilerplate-free classes.
// http://es6-features.org/#ClassDefinition
//------------------------------------------------------------------------------
class Shape {
constructor (id, x, y) {
this.id = id
this.move(x, y)
}
move (x, y) {
this.x = x
this.y = y
}
}
//------------------------------------------------------------------------------
// Class Inheritance
// More intuitive, OOP-style and boilerplate-free inheritance.
// http://es6-features.org/#ClassInheritance
//------------------------------------------------------------------------------
class Rectangle extends Shape {
constructor (id, x, y, width, height) {
super(id, x, y)
this.width = width
this.height = height
}
}
class Circle extends Shape {
constructor (id, x, y, radius) {
super(id, x, y)
this.radius = radius
}
}
//------------------------------------------------------------------------------
// Class Inheritance, From Expressions
// Support for mixin-style inheritance by extending from expressions yielding
// function objects. [Notice: the generic aggregation function is usually
// provided by a library like this one, of course]
// http://es6-features.org/#ClassInheritanceFromExpressions
//------------------------------------------------------------------------------
var aggregation = (baseClass, ...mixins) => {
let base = class _Combined extends baseClass {
constructor (...args) {
super(...args)
mixins.forEach((mixin) => {
mixin.prototype.initializer.call(this)
})
}
}
let copyProps = (target, source) => {
Object.getOwnPropertyNames(source)
.concat(Object.getOwnPropertySymbols(source))
.forEach((prop) => {
if (prop.match(/^(?:constructor|prototype|arguments|caller|name|bind|call|apply|toString|length)$/))
return
Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop))
})
}
mixins.forEach((mixin) => {
copyProps(base.prototype, mixin.prototype)
copyProps(base, mixin)
})
return base
}
class Colored {
initializer () { this._color = "white" }
get color () { return this._color }
set color (v) { this._color = v }
}
class ZCoord {
initializer () { this._z = 0 }
get z () { return this._z }
set z (v) { this._z = v }
}
class Shape {
constructor (x, y) { this._x = x; this._y = y }
get x () { return this._x }
set x (v) { this._x = v }
get y () { return this._y }
set y (v) { this._y = v }
}
class Rectangle extends aggregation(Shape, Colored, ZCoord) {}
var rect = new Rectangle(7, 42)
rect.z = 1000
rect.color = "red"
console.log(rect.x, rect.y, rect.z, rect.color)
//------------------------------------------------------------------------------
// Base Class Access
// Intuitive access to base class constructor and methods.
// http://es6-features.org/#BaseClassAccess
//------------------------------------------------------------------------------
class Shape {
// …
toString () {
return `Shape(${this.id})`
}
}
class Rectangle extends Shape {
constructor (id, x, y, width, height) {
super(id, x, y)
// …
}
toString () {
return "Rectangle > " + super.toString()
}
}
class Circle extends Shape {
constructor (id, x, y, radius) {
super(id, x, y)
// …
}
toString () {
return "Circle > " + super.toString()
}
}
//------------------------------------------------------------------------------
// Static Members
// Simple support for static class members.
// http://es6-features.org/#StaticMembers
//------------------------------------------------------------------------------
class Rectangle extends Shape {
// …
static defaultRectangle () {
return new Rectangle("default", 0, 0, 100, 100)
}
}
class Circle extends Shape {
// …
static defaultCircle () {
return new Circle("default", 0, 0, 100)
}
}
var defRectangle = Rectangle.defaultRectangle()
var defCircle = Circle.defaultCircle()
//------------------------------------------------------------------------------
// Getter/Setter
// Getter/Setter also directly within classes (and not just within object
// initializers, as it is possible since ECMAScript 5.1).
// http://es6-features.org/#GetterSetter
//------------------------------------------------------------------------------
class Rectangle {
constructor (width, height) {
this._width = width
this._height = height
}
set width (width) { this._width = width }
get width () { return this._width }
set height (height) { this._height = height }
get height () { return this._height }
get area () { return this._width * this._height }
}
var r = new Rectangle(50, 20)
r.area === 1000
//------------------------------------------------------------------------------
// Class definition with empty statement
//------------------------------------------------------------------------------
class A {
;
}
////
class B {
get [runtimeCalc]() {return 1};
set [runtimeCalc](p) {};
get 'string as key'() {};
}
// extended object
let Obj = {
[asdfg](a){},
* foo () {},
f(){},
get a(){},
set a([aa]=123){},
...anotherObj,
...{
speradObjectLiteral
},
...functionResult()
}
"use strict";
//------------------------------------------------------------------------------
// Support for constants (also known as "immutable variables"), i.e.,
// variables which cannot be re-assigned new content.
// Notice: this only makes the variable itself immutable,
// not its assigned content (for instance, in case the content is an object,
// this means the object itself can still be altered).
//------------------------------------------------------------------------------
const PI = 3.141593
PI > 3.0
var c = 10;
{
const c = 2; // At this point, c = 2.
} // At this point, c = 10.
const name = "Thomas Jefferson";
const answer = 42, numpages = 10;
const myarray = new Array();
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Array Matching
// Intuitive and flexible destructuring of Arrays into individual variables during assignment.
// http://es6-features.org/#ArrayMatching
//------------------------------------------------------------------------------
var list = [ 1, 2, 3 ]
var [ a, , b ] = list
[ b, a ] = [ a, b ]
//------------------------------------------------------------------------------
// Object Matching, Shorthand Notation
// Intuitive and flexible destructuring of Objects into individual variables during assignment.
// http://es6-features.org/#ObjectMatchingShorthandNotation
//------------------------------------------------------------------------------
var { op, lhs, rhs } = getASTNode()
//------------------------------------------------------------------------------
// Object Matching, Deep Matching
// Intuitive and flexible destructuring of Objects into individual variables during assignment.
// http://es6-features.org/#ObjectMatchingDeepMatching
//------------------------------------------------------------------------------
var { op: a, lhs: { op: b }, rhs: c } = getASTNode()
//------------------------------------------------------------------------------
// Object And Array Matching, Default Values
// Simple and intuitive default values for destructuring of Objects and Arrays.
// http://es6-features.org/#ObjectAndArrayMatchingDefaultValues
//------------------------------------------------------------------------------
var obj = { a: 1 }
var list = [ 1 ]
var { a, b = 2 } = obj
var [ x, y = 2 ] = list
//------------------------------------------------------------------------------
// Parameter Context Matching
// Intuitive and flexible destructuring of Arrays and Objects into individual
// parameters during function calls.
// http://es6-features.org/#ParameterContextMatching
//------------------------------------------------------------------------------
function f ([ name, val ]) {
console.log(name, val)
}
function g ({ name: n, val: v }) {
console.log(n, v)
}
function h ({ name, val }) {
console.log(name, val)
}
f([ "bar", 42 ])
g({ name: "foo", val: 7 })
h({ name: "bar", val: 42 })
//------------------------------------------------------------------------------
// Fail-Soft Destructuring
// Fail-soft destructuring, optionally with defaults.
// http://es6-features.org/#FailSoftDestructuring
//------------------------------------------------------------------------------
var list = [ 7, 42 ]
var [ a = 1, b = 2, c = 3, d ] = list
a === 7
b === 42
c === 3
d === undefined
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Property Shorthand
// Shorter syntax for common object property definition idiom.
// http://es6-features.org/#PropertyShorthand
//------------------------------------------------------------------------------
obj = { x, y }
//------------------------------------------------------------------------------
// Computed Property Names
// Support for computed names in object property definitions.
// http://es6-features.org/#ComputedPropertyNames
//------------------------------------------------------------------------------
let obj = {
foo: "bar",
[ "baz" + quux() ]: 42
}
var obj = {
foo: "bar"
};
obj[ "baz" + quux() ] = 42;
//------------------------------------------------------------------------------
// Method Properties
// Support for method notation in object property definitions, for both regular
// functions and generator functions.
// http://es6-features.org/#MethodProperties
//------------------------------------------------------------------------------
obj = {
foo (a, b) {
},
*quux (x, y) {
}
}
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Regular Expression Sticky Matching
// Keep the matching position sticky between matches and this way support
// efficient parsing of arbitrary long input strings, even with an arbitrary
// number of distinct regular expressions.
// http://es6-features.org/#RegularExpressionStickyMatching
//------------------------------------------------------------------------------
let parser = (input, match) => {
for (let pos = 0, lastPos = input.length; pos < lastPos; ) {
for (let i = 0; i < match.length; i++) {
match[i].pattern.lastIndex = pos
let found
if ((found = match[i].pattern.exec(input)) !== null) {
match[i].action(found)
pos = match[i].pattern.lastIndex
break
}
}
}
}
let report = (match) => {
console.log(JSON.stringify(match))
}
parser("Foo 1 Bar 7 Baz 42", [
{ pattern: /^Foo\s+(\d+)/y, action: (match) => report(match) },
{ pattern: /^Bar\s+(\d+)/y, action: (match) => report(match) },
{ pattern: /^Baz\s+(\d+)/y, action: (match) => report(match) },
{ pattern: /^\s*/y, action: (match) => {} }
])
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Binary & Octal Literal
// Direct support for safe binary and octal literals.
// http://es6-features.org/#BinaryOctalLiteral
//------------------------------------------------------------------------------
0b111110111 === 503
0o767 === 503
//------------------------------------------------------------------------------
// Unicode String & RegExp Literal
// Extended support using Unicode within strings and regular expressions.
// http://es6-features.org/#UnicodeStringRegExpLiteral
//------------------------------------------------------------------------------
"𠮷".length === 2
"𠮷".match(/./u)[0].length === 2
"𠮷" === "\uD842\uDFB7"
"𠮷" === "\u{20BB7}"
"𠮷".codePointAt(0) == 0x20BB7
for (let codepoint of "𠮷") console.log(codepoint)
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Default Parameter Values
// Simple and intuitive default values for function parameters.
// http://es6-features.org/#DefaultParameterValues
//------------------------------------------------------------------------------
function f (x, y = 7, z = 42) {
return x + y + z
}
f(1) === 50
//------------------------------------------------------------------------------
// Rest Parameter
// Aggregation of remaining arguments into single parameter of variadic functions.
// http://es6-features.org/#RestParameter
//------------------------------------------------------------------------------
function f (x, y, ...a) {
return (x + y) * a.length
}
f(1, 2, "hello", true, 7) === 9
//------------------------------------------------------------------------------
// Spread Operator
// Spreading of elements of an iterable collection (like an array or even a string)
// into both literal elements and individual function parameters.
// http://es6-features.org/#SpreadOperator
//------------------------------------------------------------------------------
var params = [ "hello", true, 7 ]
var other = [ 1, 2, ...params ] // [ 1, 2, "hello", true, 7 ]
function f (x, y, ...a) {
return (x + y) * a.length
}
f(1, 2, ...params) === 9
var str = "foo"
var chars = [ ...str ] // [ "f", "o", "o" ]
\ No newline at end of file
f();
f(a);
f(...a);
f(a,);
f(1,2,...[3]);
f(4,...a,...[b],);
"use strict";
//------------------------------------------------------------------------------
// Generator Function, Iterator Protocol
// Support for generators, a special case of Iterators containing a generator
// function, where the control flow can be paused and resumed, in order to
// produce sequence of values (either finite or infinite).
// http://es6-features.org/#GeneratorFunctionIteratorProtocol
//------------------------------------------------------------------------------
let fibonacci = {
*[Symbol.iterator]() {
let pre = 0, cur = 1
for (;;) {
[ pre, cur ] = [ cur, pre + cur ]
yield cur
}
}
}
for (let n of fibonacci) {
if (n > 1000)
break
console.log(n)
}
//------------------------------------------------------------------------------
// Generator Function, Direct Use
// Support for generator functions, a special variant of functions where the
// control flow can be paused and resumed, in order to produce sequence of
// values (either finite or infinite).
// http://es6-features.org/#GeneratorFunctionDirectUse
//------------------------------------------------------------------------------
function* range (start, end, step) {
while (start < end) {
yield start
start += step
}
}
for (let i of range(0, 10, 2)) {
console.log(i) // 0, 2, 4, 6, 8
}
//------------------------------------------------------------------------------
// Generator Matching
// Support for generator functions, i.e., functions where the control flow can
// be paused and resumed, in order to produce and spread sequence of values
// (either finite or infinite).
// http://es6-features.org/#GeneratorMatching
//------------------------------------------------------------------------------
let fibonacci = function* (numbers) {
let pre = 0, cur = 1
while (numbers-- > 0) {
[ pre, cur ] = [ cur, pre + cur ]
yield cur
}
}
for (let n of fibonacci(1000))
console.log(n)
let numbers = [ ...fibonacci(1000) ]
let [ n1, n2, n3, ...others ] = fibonacci(1000)
//------------------------------------------------------------------------------
// Generator Control-Flow
// Support for generators, a special case of Iterators where the control flow
// can be paused and resumed, in order to support asynchronous programming in
// the style of "co-routines" in combination with Promises (see below).
// [Notice: the generic async function usually is provided by a reusable
// library and given here just for better understanding. See co or Bluebird's
// coroutine in practice.]
// http://es6-features.org/#GeneratorControlFlow
//------------------------------------------------------------------------------
// async is a keyword now...
// generic asynchronous control-flow driver
function fasync (proc, ...params) {
var iterator = proc(...params)
return new Promise((resolve, reject) => {
let loop = (value) => {
let result
try {
result = iterator.next(value)
}
catch (err) {
reject(err)
}
if (result.done)
resolve(result.value)
else if ( typeof result.value === "object"
&& typeof result.value.then === "function")
result.value.then((value) => {
loop(value)
}, (err) => {
reject(err)
})
else
loop(result.value)
}
loop()
})
}
// application-specific asynchronous builder
function makeAsync (text, after) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(text), after)
})
}
// application-specific asynchronous procedure
fasync(function* (greeting) {
let foo = yield makeAsync("foo", 300)
let bar = yield makeAsync("bar", 200)
let baz = yield makeAsync("baz", 100)
return `${greeting} ${foo} ${bar} ${baz}`
}, "Hello").then((msg) => {
console.log("RESULT:", msg) // "Hello foo bar baz"
})
//------------------------------------------------------------------------------
// Generator Methods
// Support for generator methods, i.e., methods in classes and on objects, based on generator functions.
// http://es6-features.org/#GeneratorMethods
//------------------------------------------------------------------------------
class Clz {
* bar () {
// …
}
}
let Obj = {
* foo () {
// …
}
}
"use strict";
//------------------------------------------------------------------------------
// Collation
// Sorting a set of strings and searching within a set of strings. Collation is
// parameterized by locale and aware of Unicode.
// http://es6-features.org/#Collation
//------------------------------------------------------------------------------
// in German, "ä" sorts with "a"
// in Swedish, "ä" sorts after "z"
var list = [ "ä", "a", "z" ]
var l10nDE = new Intl.Collator("de")
var l10nSV = new Intl.Collator("sv")
l10nDE.compare("ä", "z") === -1
l10nSV.compare("ä", "z") === +1
console.log(list.sort(l10nDE.compare)) // [ "a", "ä", "z" ]
console.log(list.sort(l10nSV.compare)) // [ "a", "z", "ä" ]
//------------------------------------------------------------------------------
// Number Formatting
// Format numbers with digit grouping and localized separators.
// http://es6-features.org/#NumberFormatting
//------------------------------------------------------------------------------
var l10nEN = new Intl.NumberFormat("en-US")
var l10nDE = new Intl.NumberFormat("de-DE")
l10nEN.format(1234567.89) === "1,234,567.89"
l10nDE.format(1234567.89) === "1.234.567,89"
//------------------------------------------------------------------------------
// Currency Formatting
// Format numbers with digit grouping, localized separators and attached currency symbol.
// http://es6-features.org/#CurrencyFormatting
//------------------------------------------------------------------------------
var l10nUSD = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })
var l10nGBP = new Intl.NumberFormat("en-GB", { style: "currency", currency: "GBP" })
var l10nEUR = new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" })
l10nUSD.format(100200300.40) === "$100,200,300.40"
l10nGBP.format(100200300.40) === "£100,200,300.40"
l10nEUR.format(100200300.40) === "100.200.300,40 €"
//------------------------------------------------------------------------------
// Date/Time Formatting
// Format date/time with localized ordering and separators.
// http://es6-features.org/#DateTimeFormatting
//------------------------------------------------------------------------------
var l10nEN = new Intl.DateTimeFormat("en-US")
var l10nDE = new Intl.DateTimeFormat("de-DE")
l10nEN.format(new Date("2015-01-02")) === "1/2/2015"
l10nDE.format(new Date("2015-01-02")) === "2.1.2015"
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Iterator & For-Of Operator
// http://es6-features.org/#IteratorForOfOperator
// http://es6-features.org/#IteratorForOfOperator
//------------------------------------------------------------------------------
let fibonacci = {
[Symbol.iterator]() {
let pre = 0, cur = 1
return {
next () {
[ pre, cur ] = [ cur, pre + cur ]
return { done: false, value: cur }
}
}
}
}
for (let n of fibonacci) {
if (n > 1000)
break
console.log(n)
}
"use strict";
//------------------------------------------------------------------------------
// Map/Set & WeakMap/WeakSet
// Cleaner data-structure for common algorithms based on sets.
// http://es6-features.org/#SetDataStructure
//------------------------------------------------------------------------------
let s = new Set()
s.add("hello").add("goodbye").add("hello")
s.size === 2
s.has("hello") === true
for (let key of s.values()) // insertion order
console.log(key)
//------------------------------------------------------------------------------
// Map Data-Structure
// Cleaner data-structure for common algorithms based on maps.
// http://es6-features.org/#MapDataStructure
//------------------------------------------------------------------------------
let m = new Map()
let s = Symbol()
m.set("hello", 42)
m.set(s, 34)
m.get(s) === 34
m.size === 2
for (let [ key, val ] of m.entries())
console.log(key + " = " + val)
//------------------------------------------------------------------------------
// Weak-Link Data-Structures
// Memory-leak-free Object-key’d side-by-side data-structures.
// http://es6-features.org/#WeakLinkDataStructures
//------------------------------------------------------------------------------
let isMarked = new WeakSet()
let attachedData = new WeakMap()
export class Node {
constructor (id) { this.id = id }
mark () { isMarked.add(this) }
unmark () { isMarked.delete(this) }
marked () { return isMarked.has(this) }
set data (data) { attachedData.set(this, data) }
get data () { return attachedData.get(this) }
}
let foo = new Node("foo")
JSON.stringify(foo) === '{"id":"foo"}'
foo.mark()
foo.data = "bar"
foo.data === "bar"
JSON.stringify(foo) === '{"id":"foo"}'
isMarked.has(foo) === true
attachedData.has(foo) === true
foo = null /* remove only reference to foo */
attachedData.has(foo) === false
isMarked.has(foo) === false
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Proxying
// Hooking into runtime-level object meta-operations.
// http://es6-features.org/#Proxying
//------------------------------------------------------------------------------
let target = {
foo: "Welcome, foo"
}
let proxy = new Proxy(target, {
get (receiver, name) {
return name in receiver ? receiver[name] : `Hello, ${name}`
}
})
proxy.foo === "Welcome, foo"
proxy.world === "Hello, world"
//------------------------------------------------------------------------------
// Reflection
// Make calls corresponding to the object meta-operations.
// http://es6-features.org/#Reflection
//------------------------------------------------------------------------------
let obj = { a: 1 }
Object.defineProperty(obj, "b", { value: 2 })
obj[Symbol("c")] = 3
Reflect.ownKeys(obj) // [ "a", "b", Symbol(c) ]
\ No newline at end of file
var e\u0065e;
var e\u{0065}e; // var eee;
var Π;
// var 全世界无产者联合起来; //UTF8
var \u4e00;
0b0n;
12345n;
0x1234n;
12_123;
1.123_3456e-7_8
0b0_1n;
try {}
catch{}
finally{}
try {}
finally{}
2**3*5;
async function process(array) {
for await (let i of array) {
doSomething(i);
}
}
const {a, ...c} = obj;
const obj2 = { ...obj1, z: 26 ,...obj2};
const [aa,b,...ca] = aarr;
const v = [asdf,...gh,jk];
var f = [,,c,...a,b,,];
var g = [,,]; // [undefined,undefined]
function f([asdfg]=12345){}
/* TODO:
function async (proc, ...params) {}
var async = 1;*/
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Value Export/Import
// Support for exporting/importing values from/to modules without global namespace pollution.
// http://es6-features.org/#ValueExportImport
//------------------------------------------------------------------------------
// lib/math.js
export function sum (x, y) { return x + y }
export var pi = 3.141593
// someApp.js
import * as math from "lib/math"
console.log("2π = " + math.sum(math.pi, math.pi))
// otherApp.js
import { sum, pi } from "lib/math"
console.log("2π = " + sum(pi, pi))
//------------------------------------------------------------------------------
// Default & Wildcard
// Marking a value as the default exported value and mass-mixin of values.
// http://es6-features.org/#DefaultWildcard
//------------------------------------------------------------------------------
// lib/mathplusplus.js
export * from "lib/math"
export var e = 2.71828182846
export default (x) => Math.exp(x)
// someApp.js
import exp, { pi, e } from "lib/mathplusplus"
console.log("e^{π} = " + exp(pi))
export {};
import a,{aaa as bbb} from 'ccc';
import('runtime_import');
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Object Property Assignment
// New function for assigning enumerable properties of one or more source
// objects onto a destination object.
// http://es6-features.org/#ObjectPropertyAssignment
//------------------------------------------------------------------------------
var dest = { quux: 0 }
var src1 = { foo: 1, bar: 2 }
var src2 = { foo: 3, baz: 4 }
Object.assign(dest, src1, src2)
dest.quux === 0
dest.foo === 3
dest.bar === 2
dest.baz === 4
//------------------------------------------------------------------------------
// Array Element Finding
// New function for finding an element in an array.
// http://es6-features.org/#ArrayElementFinding
//------------------------------------------------------------------------------
[ 1, 3, 4, 2 ].find(x => x > 3) // 4
[ 1, 3, 4, 2 ].findIndex(x => x > 3) // 2
//------------------------------------------------------------------------------
// String Repeating
// New string repeating functionality.
// http://es6-features.org/#StringRepeating
//------------------------------------------------------------------------------
" ".repeat(4 * depth)
"foo".repeat(3)
//------------------------------------------------------------------------------
// String Searching
// New specific string functions to search for a sub-string.
// http://es6-features.org/#StringSearching
//------------------------------------------------------------------------------
"hello".startsWith("ello", 1) // true
"hello".endsWith("hell", 4) // true
"hello".includes("ell") // true
"hello".includes("ell", 1) // true
"hello".includes("ell", 2) // false
//------------------------------------------------------------------------------
// Number Type Checking
// New functions for checking for non-numbers and finite numbers.
// http://es6-features.org/#NumberTypeChecking
//------------------------------------------------------------------------------
Number.isNaN(42) === false
Number.isNaN(NaN) === true
Number.isFinite(Infinity) === false
Number.isFinite(-Infinity) === false
Number.isFinite(NaN) === false
Number.isFinite(123) === true
//------------------------------------------------------------------------------
// Number Safety Checking
// Checking whether an integer number is in the safe range, i.e., it is correctly
// represented by JavaScript (where all numbers, including integer numbers,
// are technically floating point number).
// http://es6-features.org/#NumberSafetyChecking
//------------------------------------------------------------------------------
Number.isSafeInteger(42) === true
Number.isSafeInteger(9007199254740992) === false
//------------------------------------------------------------------------------
// Number Comparison
// Availability of a standard Epsilon value for more precise comparison of
// floating point numbers.
// http://es6-features.org/#NumberComparison
//------------------------------------------------------------------------------
console.log(0.1 + 0.2 === 0.3) // false
console.log(Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON) // true
//------------------------------------------------------------------------------
// Number Truncation
// Truncate a floating point number to its integral part, completely dropping
// the fractional part.
// http://es6-features.org/#NumberTruncation
//------------------------------------------------------------------------------
console.log(Math.trunc(42.7)) // 42
console.log(Math.trunc( 0.1)) // 0
console.log(Math.trunc(-0.1)) // -0
//------------------------------------------------------------------------------
// Number Sign Determination
// Determine the sign of a number, including special cases of signed zero and non-number.
// http://es6-features.org/#NumberSignDetermination
//------------------------------------------------------------------------------
console.log(Math.sign(7)) // 1
console.log(Math.sign(0)) // 0
console.log(Math.sign(-0)) // -0
console.log(Math.sign(-7)) // -1
console.log(Math.sign(NaN)) // NaN
\ No newline at end of file
//------------------------------------------------------------------------------
// Html Comment
//------------------------------------------------------------------------------
<!--//-->
//------------------------------------------------------------------------------
// CDATA Section
//------------------------------------------------------------------------------
<![CDATA[//><!--//--><!]]>
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Promise Usage
// First class representation of a value that may be made asynchronously and
// be available in the future.
// http://es6-features.org/#PromiseUsage
//------------------------------------------------------------------------------
function msgAfterTimeout (msg, who, timeout) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(`${msg} Hello ${who}!`), timeout)
})
}
msgAfterTimeout("", "Foo", 100).then((msg) =>
msgAfterTimeout(msg, "Bar", 200)
).then((msg) => {
console.log(`done after 300ms:${msg}`)
})
//------------------------------------------------------------------------------
// Promise Combination
// Combine one or more promises into new promises without having to take care
// of ordering of the underlying asynchronous operations yourself.
// http://es6-features.org/#PromiseCombination
//------------------------------------------------------------------------------
function fetchAsync (url, timeout, onData, onError) {
// …
}
let fetchPromised = (url, timeout) => {
return new Promise((resolve, reject) => {
fetchAsync(url, timeout, resolve, reject)
})
}
Promise.all([
fetchPromised("http://backend/foo.txt", 500),
fetchPromised("http://backend/bar.txt", 500),
fetchPromised("http://backend/baz.txt", 500)
]).then((data) => {
let [ foo, bar, baz ] = data
console.log(`success: foo=${foo} bar=${bar} baz=${baz}`)
}, (err) => {
console.log(`error: ${err}`)
})
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Block-Scoped Variables
// Block-scoped variables (and constants) without hoisting.
// http://es6-features.org/#BlockScopedVariables
//------------------------------------------------------------------------------
for (let i = 0; i < a.length; i++) {
let x = a[i]
}
for (let i = 0; i < b.length; i++) {
let y = b[i]
}
let callbacks = []
for (let i = 0; i <= 2; i++) {
callbacks[i] = function () { return i * 2 }
}
callbacks[0]() === 0
callbacks[1]() === 2
callbacks[2]() === 4
//------------------------------------------------------------------------------
// Block-Scoped Functions
// Block-scoped function definitions.
// http://es6-features.org/#BlockScopedFunctions
//------------------------------------------------------------------------------
function foo () { return 1 }
foo() === 1
{
function foo () { return 2 }
foo() === 2
}
#! /usr/bin/env node
// class field proposal, stage3 (2019/11/03)
class CF {
#x = 12345;
y = id;
#priv_method(){
this.#x++;
}
async * #method() {
}
#priv_var = 9961;
}
var a = b??c;
var d = e?.f?.g;
var h = i?.j();
\ No newline at end of file
function strict() {
/* comment */
// comment
// comment
'use strict';
function nested()
{
let a = 4; // strict, "let" is not an identifier.
return 'And so am I!';
}
return "Hi! I'm a strict mode function! " + nested();
}
function notStrict() {
var let = 0; // not strict, no error.
return "I'm not strict.";
}
\ No newline at end of file
// comment before use strict
"use strict";
let a = 42;
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Symbol Type
// Unique and immutable data type to be used as an identifier for object
// properties. Symbol can have an optional description, but for debugging
// purposes only.
// http://es6-features.org/#SymbolType
//------------------------------------------------------------------------------
Symbol("foo") !== Symbol("foo")
const foo = Symbol()
const bar = Symbol()
typeof foo === "symbol"
typeof bar === "symbol"
let obj = {}
obj[foo] = "foo"
obj[bar] = "bar"
JSON.stringify(obj) // {}
Object.keys(obj) // []
Object.getOwnPropertyNames(obj) // []
Object.getOwnPropertySymbols(obj) // [ foo, bar ]
//------------------------------------------------------------------------------
// Global Symbols
// Global symbols, indexed through unique keys.
// http://es6-features.org/#GlobalSymbols
//------------------------------------------------------------------------------
Symbol.for("app.foo") === Symbol.for("app.foo")
const foo = Symbol.for("app.foo")
const bar = Symbol.for("app.bar")
Symbol.keyFor(foo) === "app.foo"
Symbol.keyFor(bar) === "app.bar"
typeof foo === "symbol"
typeof bar === "symbol"
let obj = {}
obj[foo] = "foo"
obj[bar] = "bar"
JSON.stringify(obj) // {}
Object.keys(obj) // []
Object.getOwnPropertyNames(obj) // []
Object.getOwnPropertySymbols(obj) // [ foo, bar ]
\ No newline at end of file
"use strict";
// TODO: implement interpolation expressions parsing.
//------------------------------------------------------------------------------
// String Interpolation
// Intuitive expression interpolation for single-line and multi-line strings.
// (Notice: don't be confused, Template Literals were originally named
// "Template Strings" in the drafts of the ECMAScript 6 language specification)
// http://es6-features.org/#StringInterpolation
//------------------------------------------------------------------------------
var customer = { name: "Foo" }
var card = { amount: 7, product: "Bar", unitprice: 42 }
var message = `Hello ${customer.name},
want to buy ${card.amount} ${card.product} for
a total of ${card.amount * card.unitprice} bucks?`
//------------------------------------------------------------------------------
// Custom Interpolation
// Flexible expression interpolation for arbitrary methods.
// http://es6-features.org/#CustomInterpolation
//------------------------------------------------------------------------------
get`http://example.com/foo?bar=${bar + baz}&quux=${quux}`
//------------------------------------------------------------------------------
// Raw String Access
// Access the raw template string content (backslashes are not interpreted).
// http://es6-features.org/#RawStringAccess
//------------------------------------------------------------------------------
function quux (strings, ...values) {
strings[0] === "foo\n"
strings[1] === "bar"
strings.raw[0] === "foo\\n"
strings.raw[1] === "bar"
values[0] === 42
}
quux `foo\n${ 42 }bar`
String.raw `foo\n${ 42 }bar` === "foo\\n42bar"
\ No newline at end of file
"use strict";
//------------------------------------------------------------------------------
// Typed Arrays
// Support for arbitrary byte-based data structures to implement network protocols,
// cryptography algorithms, file format manipulations, etc.
// http://es6-features.org/#TypedArrays
//------------------------------------------------------------------------------
// ES6 class equivalent to the following C structure:
// struct Example { unsigned long id; char username[16]; float amountDue }
class Example {
constructor (buffer = new ArrayBuffer(24)) {
this.buffer = buffer
}
set buffer (buffer) {
this._buffer = buffer
this._id = new Uint32Array (this._buffer, 0, 1)
this._username = new Uint8Array (this._buffer, 4, 16)
this._amountDue = new Float32Array(this._buffer, 20, 1)
}
get buffer () { return this._buffer }
set id (v) { this._id[0] = v }
get id () { return this._id[0] }
set username (v) { this._username[0] = v }
get username () { return this._username[0] }
set amountDue (v) { this._amountDue[0] = v }
get amountDue () { return this._amountDue[0] }
}
let example = new Example()
example.id = 7
example.username = "John Doe"
example.amountDue = 42.0
\ No newline at end of file
abstract class Person {
name: string;
constructor(name: string) {
this.name = name;
}
abstract find(string): Person;
abstract nameAbs: string;
}
class Employee extends Person {
empCode: number;
constructor(name: string, code: number) {
super(name); // must call super()
this.empCode = code;
}
find(name:string): Person {
// execute AJAX request to find an employee from a db
return new Employee(name, 1);
}
}
let emp: Person = new Employee("James", 100);
emp.display(); //James
let emp2: Person = emp.find('Steve');
\ No newline at end of file
interface IPerson {
name: string;
}
class Person implements IPerson {
public publicString: string;
private privateString: string;
protected protectedString: string;
readonly readonlyString: string;
name: string;
constructor(name: string) {
this.name = name;
}
}
class Employee extends Person {
empCode: number;
static pi: number = 3.14;
constructor(empcode: number, name:string) {
super(name);
this.empCode = empcode;
}
displayName():void {
console.log("Name = " + this.name + ", Employee Code = " + this.empCode);
}
}
let emp = new Employee(100,"Steve");
\ No newline at end of file
namespace StringUtility
{
function ToCapital(str: string): string {
return str.toUpperCase();
}
function Nemw(str: string, length: number = 0): string {
return str.toUpperCase();
}
export function Eported(from: string, length: number = 0): string {
return from.toUpperCase();
}
export function Eported2(str: string, length: number = 0): string {
return str.toUpperCase();
}
}
\ No newline at end of file
function Sum(x: number, y: number) : void {
console.log('processNumKeyPairs: key = ' + key + ', value = ' + value)
return x + y;
}
let greeting = function() {
console.log("Hello TypeScript!");
};
let SumAnon = function(x: number, y: number) : number
{
return x + y;
}
function Greet(greeting: string, name?: string ) : string {
return greeting + ' ' + name + '!';
}
function Greet2(name: string, greeting: string = "Hello") : string {
return greeting + ' ' + name + '!';
}
Greet(undefined, 'Steve');
let sumArrow = (x: number, y: number): number => {
return x + y
}
let Print = () => console.log("Hello TypeScript");
let sumShortArrow = (x: number, y: number) => x + y;
function Greet(greeting: string, ...names: string[]) {
return greeting + " " + names.join(", ") + "!";
}
function Test(value: TestClass | TestClass2): value is TestClass {
return (<TestClass>value).someFunction !== undefined;
}
\ No newline at end of file
function getArray<T>(items : T[] ) : T[] {
return new Array<T>().concat();
}
let myNumArr = getArray<Test>([100, 200, 300]);
let myStrArr = getArray<string>(["Hello", "World"]);
myNumArr.push(400); // OK
myStrArr.push("Hello TypeScript"); // OK
myNumArr.push("Hi"); // Compiler Error
myStrArr.push(500); // Compiler Error
function displayType<T, U>(id:T, name:U): void {
console.log(typeof(id) + ", " + typeof(name));
}
function displayTypeNon<T>(id:T, name:string): void {
console.log(typeof(id) + ", " + typeof(name));
}
function displayNames<T>(names:T[]): void {
console.log(names.join(", "));
}
function display<T extends Person>(per: T): void {
console.log(`${ per.firstName} ${per.lastName}` );
}
\ No newline at end of file
class KeyValuePair<T,U>
{
private key: T;
private val: U;
setKeyValue(key: T, val: U): void {
this.key = key;
this.val = val;
}
display():void {
console.log(`Key = ${this.key}, val = ${this.val}`);
}
}
let kvp1 = new KeyValuePair<number, string>();
kvp1.setKeyValue(1, "Steve");
kvp1.display(); //Output: Key = 1, Val = Steve
let kvp2 = new KayValuePair<string, string>();
kvp2.SetKeyValue("CEO", "Bill");
kvp2.display(); //Output: Key = CEO, Val = Bill
interface IKeyValueProcessor<T, U>
{
process(key: T, val: U): void;
};
class kvProcessor<T, U> implements IKeyValueProcessor<T, U>
{
process(key:T, val:U):void {
console.log(`Key = ${key}, val = ${val}`);
}
}
\ No newline at end of file
interface IProcessor
{
result:T;
process(a: T, b: T) => T;
}
interface KeyPair<T, U> {
key: T;
value: U;
}
let kv1: KeyPair<number, string> = { key:1, value:"Steve" }; // OK
let kv2: KeyPair<number, number> = { key:1, value:12345 }; // OK
interface KeyValueProcessor<T, U>
{
(key: T, val: U): void;
};
function processNumKeyPairs(key:number, value:number):void {
console.log('processNumKeyPairs: key = ' + key + ', value = ' + value)
}
\ No newline at end of file
export interface IPerson {
name: string;
gender: string;
}
interface IEmployee extends IPerson{
empCode: number;
readonly empName: string;
empDept?:string;
getSalary: (number) => number; // arrow function
getManagerName(number): string;
}
class Employee implements IEmployee {
empCode: number;
name: string;
constructor(code: number, name: string) {
this.empCode = code;
this.name = name;
}
getSalary(empCode:number):number {
return 20000;
}
}
// Interface As type
interface KeyPair {
key: number;
value: string;
}
let kv1: KeyPair = { key:1, value:"Steve" }; // OK
let kv2: KeyPair = { key:1, val:"Steve" }; // Compiler Error: 'val' doesn't exist in type 'KeyPair'
let kv3: KeyPair = { key:1, value:100 }; // Compiler Error
//Interface for Array Type
interface NumList {
[index:string]:string
}
let numArr: NumList = [1, 2, 3];
numArr[0];
numArr[1];
interface IStringList {
[index:string]:string
}
let strArr : IStringList;
strArr["TS"] = "TypeScript";
strArr["JS"] = "JavaScript";
\ No newline at end of file
export let age : number = 20;
export class Employee {
empCode: number;
empName: string;
constructor(name: string, code: number) {
this.empName = name;
this.empCode = code;
}
displayEmployee() {
console.log ("Employee Code: " + this.empCode + ", Employee Name: " + this.empName );
}
}
let companyName:string = "XYZ";
\ No newline at end of file
let x: number = 10, y = 20;
// If-else
if (x > y)
{
console.log('x is greater than y.');
}
else
{
console.log('x is less than or equal to y.'); //This will be executed
}
// Switch
switch (x-y) {
case 0:
console.log("Result: 0");
break;
case 5:
console.log("Result: 5");
break;
case 10:
console.log("Result: 10");
break;
}
// For
for (let i = 0; i < 3; i++) {
console.log ("Block statement execution no." + i);
}
let str = "Hello World";
for (var char of str) {
console.log(char); // prints chars: H e l l o W o r l d
}
let arr = [10, 20, 30, 40];
for (var index in arr) {
console.log(index); // prints indexes: 0, 1, 2, 3
console.log(arr[index]); // prints elements: 10, 20, 30, 40
}
// While
let i: number = 2;
do {
console.log("Block statement execution no." + i )
i++;
} while ( i < 4)
while (i < 4) {
console.log( "Block statement execution no." + i )
i++;
}
\ No newline at end of file
// TypeAnotation
var age: number = 32; // number variable
var name: string = "John";// string variable
var isUpdated: boolean = true;// Boolean variable
var employee : {
id: number;
name: string;
};
employee = {
id: 100,
name : "John"
}
function display(id:number, name:string)
{
console.log("Id = " + id + ", Name = " + name);
}
// TypeInterface
var arr = [0, 1, "test"];
arr.push("str") // OK
arr.push(true); // Compiler Error: Argument of type 'true' is not assignable to parameter of type 'string | number'
function sum(a: number, b: number )
{
return a + b;
}
var total: number = sum(10,20); // OK
var str: string = sum(10,20); // Compiler Error
// TypeAssertion
let code: any = 123;
let employeeCode = <number> code;
console.log(typeof(employeeCode)); //Output: number
let employeeName = "John";
// or
let employeeName:string = "John";
var num1:number = 1;
const playerCodes = {
player1 : 9,
player2 : 10,
player3 : 13,
player4 : 20
};
playerCodes.player2 = 11; // OK
playerCodes = { //Compiler Error: Cannot assign to playerCodes because it is a constant or read-only
player1 : 50, // Modified value
player2 : 10,
player3 : 13,
player4 : 20
};
playerCodesArray = { //Compiler Error: Cannot assign to playerCodes because it is a constant or read-only
player1 : 50, // Modified value
player2 : playerCodes[Test],
player3 : 13,
player4 : 20
};
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册