ts-learnings/11_symbols.md
2019-03-27 11:20:25 +08:00

4.3 KiB
Raw Blame History

符号

Symbols

简介

从ECMAScript 2015开始numberstring一样,symbol就是一种原生数据类型了。

symbol值的创建,是通过调用Symbol构造器完成的。

let sym1 = Symbol();
let sym2 = Symbol("key"); // “key”是可选字符串

符号是不可修改的,且具独特性。

let sym2 = Symbol("key");
let sym3 = Symbol("key");

sym2 === sym3; // `false`,因为符号是独特的

与字符串一样,符号可作为对象属性的键来使用。

let sym = Symbol();

let obj = {
    [sym]: "value"
};

console.log(obj[sym]); // "value"

符号也可与 计算属性声明 一起来声明对象属性与类成员Symbols can also be combined with computed property declarations to declare object properties and class members

const getClassNameSymbol = Symbol();

class C {
    [getClassNameSymbol](){
        return "C";
    }
}

let c = new C();
let className = c[getClassNameSymbol](); // "C"

一些熟知的符号Well-known Symbols

出来用户定义的符号外还有一些内建熟知的符号。内建的符号用于表示内部的语言行为In addition to user-defined symbols, there are Well-known built-in symbols. Built-in symbols are used to represent internal language behaviors

下面是一个熟知符号的清单。

Symbol.hasInstance

判断构造器对象是否将某个对象识别为该构造器的一个实例的方法。由instanceof运算符的语义调用A method that determines if a constructor object recognizes an object as one of the constructor's insstance. Called by the semantics of the instanceof operator

symbol.isConcatSpreadable

一个表明某对象应通过Array.prototype.concat被扁平化到其数组元素的逻辑值A Boolean value indicating taht an object should be flattened to its array elements by Array.prototype.concat)。

Symbol.iterator

返回某对象默认迭代器的方法。由for .. of语句的语义调用A method that returns the default iterator for an object. Called by the semantics of the for .. in statement

Symbol.match

一个对正则表达式与字符串进行匹配的正则表达式函数。由String.prototype.match方法进行调用A regular expression method that matches the regular expression against a string. Called by the String.prototype.match method

Symbol.replace

一个对字符串中匹配的子字符串进行匹配的正则表达式函数。由String.prototype.replace方法进行调用A regular expression method that replaces matched substring of a string. Called by the String.prototype.replace method

Symbol.search

一个返回与正则表达式匹配的某字符串中索引的正则表达式函数。由String.prototype.search方法调用A regular expression method that returns the index within a string that matches the regular expression. Called by the String.prototype.search method

Symbol.species

一个用于创建派生对象的函数值属性也就是构造函数A function valued property that is the constructor function that is used to create derived objects

Symbol.split

一个在与给出的正则表达式匹配的索引出对字符串进行分割的正则表达式函数。由String.prototype.split方法进行调用A regular expression method that splits a string at the indices that match the regular expression. Called by the String.prototype.split method

Symbol.toPrimitive

一个将对象转换到相应的原生值的方法。由ToPrimitive抽象操作进行调用A method that converts an object to a corresponding primitive value. Called by the ToPrimitive abstract operation

Symbol.toStringTag

一个在对象默认字符串说明的创建中用到的字符串值。由内建的Object.prototype.toString方法调用A string value that is used in the creation of the default string description of an object. Called by the built-in method Object.prototype.toString)。

Symbol.unscopables

一个其自身属性名称是那些排除在相关对象的with环境绑定之外的属性名称的对象An Object whose own property names are property names excluded from the with environment bindings of the associated objects