标签模板:
String.raw(callSite, ...substitutions) : string
用于获取“原始”字符串内容的模板标签(反斜杠不再是转义字符):
> String.raw`\` === ‘\\‘
true
Unicode 和码点:
String.fromCodePoint(...codePoints : number[]) : string
将数字值转换成 Unicode 码点字,然后返回由码点构成的字符串。
String.prototype.codePointAt(pos) : number
返回在从位置 pos 处开始的码点的数字值(由一个或者两个 JavaScript 字符组成)。
String.prototype.normalize(form? : string) : string
不同的码点组合可能看起来是一样的。 Unicode 标准化 将它们修正为相同的标准值。这对相等比较和字符串搜索很有帮助。对于一般的文本,建议使用 NFC形式。
查找字符串:
String.prototype.startsWith(searchString, position=0) : boolean
position 参数指定了字符串的开始搜索位置。
String.prototype.endsWith(searchString, endPosition=searchString.length) : boolean
endPosition 指定了字符串的结束搜索位置。
String.prototype.includes(searchString, position=0) : boolean
从字符串 position 位置开始搜索,是否包含 searchString 子串。
重复字符串:
String.prototype.repeat(count) : string
返回重复指定次数的字符串。
Symbol 主要用作唯一属性键 - 一个 symbol 对象不会与任何其他属性键(另一个 symbol 对象或者字符串)冲突。例如,你可以将 Symbol.iterator 作为某个对象的键(键值是一个方法),使其变得可迭代(可以通过 for-of 或者其他语言机制来迭代,更多相关内容可以在有关迭代的章节找到):
const iterableObject = { [Symbol.iterator]() { // (A) const data = [‘hello‘, ‘world‘]; let index = 0; return { next() { if (index < data.length) { return { value: data[index++] }; } else { return { done: true }; } } }; } } for (const x of iterableObject) { console.log(x); } // Output: // hello // world
在行 A 处, symbol 对象用作键,键值是一个方法。这个唯一的键使得该对象可迭代,可用于 for-of 循环。
在 ECMAScript 5 中,你可能会使用字符串来表示一些特殊的含义,比如颜色。在 ES6 中,可以使用 symbol ,因为 symbol 总是唯一的:
const COLOR_RED = Symbol(‘Red‘); const COLOR_ORANGE = Symbol(‘Orange‘); const COLOR_YELLOW = Symbol(‘Yellow‘); const COLOR_GREEN = Symbol(‘Green‘); const COLOR_BLUE = Symbol(‘Blue‘); const COLOR_VIOLET = Symbol(‘Violet‘); function getComplement(color) { switch (color) { case COLOR_RED: return COLOR_GREEN; case COLOR_ORANGE: return COLOR_BLUE; case COLOR_YELLOW: return COLOR_VIOLET; case COLOR_GREEN: return COLOR_RED; case COLOR_BLUE: return COLOR_ORANGE; case COLOR_VIOLET: return COLOR_YELLOW; default: throw new Exception(‘Unknown color: ‘+color); } }
将 symbol 强制(隐式地)转换成字符串会抛出异常:
const sym = Symbol(‘desc‘); const str1 = ‘‘ + sym; // TypeError const str2 = `${sym}`; // TypeError
只能通过显示的方式转换:
const str2 = String(sym); // ‘Symbol(desc)‘ const str3 = sym.toString(); // ‘Symbol(desc)‘
禁止强制转换能够避免一些错误,但是也使得 symbol 的使用变得复杂起来。
下面的操作能感知到 symbol 键:
Reflect.ownKeys()[] 访问属性Object.assign()下面的操作会忽略掉 symbol 键:
Object.keys()Object.getOwnPropertyNames()for-in 循环ES6 新增了两个定义变量的关键字:let 与 const,它们几乎取代了 ES5 定义变量的方式:var。
letlet 语法上非常类似于 var,但定义的变量是语句块级作用域,只存在于当前的语句块中。var 拥有函数作用域。
在如下的代码中,let 定义的变量 tmp 只存在于行 A 开始的语句块中:
function order(x, y) {
if (x > y) { // (A)
let tmp = x;
x = y;
y = tmp;
}
console.log(tmp === x); // ReferenceError: tmp is not defined
return [x, y];
}
constconst 和 let 类似,但是定义变量时必须初始化值,并且是只读的。
const foo;
// SyntaxError: missing = in const declaration
const bar = 123;
bar = 456;
// TypeError: `bar` is read-only
下面的表格对比了在 ES6 中定义变量的 6 种方式:
| Hoisting | Scope | Creates global properties | |||
|---|---|---|---|---|---|
| var | Declaration | Function | Yes | ||
| let | Temporal | dead | zone | Block | No |
| const | Temporal | dead | zone | Block | No |
| function | Complete | Block | Yes | ||
| class | No | Block | No | ||
| import | Complete | Module-global | No |
原文:http://www.cnblogs.com/huansky/p/5669880.html