1. 语言类型和规范类型
6 ECMAScript Data Types and Values
Algorithms within this specification manipulate values each of which has an associated type. The possible value types are exactly those defined in this clause. Types are further subclassified into ECMAScript language types and specification types.
ECMAScript规范指出,每一个值都有其相应的类型,而类型又可以分为两种,
(1)ECMAScript language types
(2)Specification types
6.1 ECMAScript Language Types
An ECMAScript language type corresponds to values that are directly manipulated by an ECMAScript programmer using the ECMAScript language. The ECMAScript language types are Undefined, Null, Boolean, String, Symbol, Number, and Object. An ECMAScript language value is a value that is characterized by an ECMAScript language type.
ECMAScript language type是ECMAScript的程序员在编写程序时可以操作的那些类型,包括7种,
Undefined
,Null
,Boolean
,String
,Symbol
,Number
,Object
。
6.2 ECMAScript Specification Types
A specification type corresponds to meta-values that are used within algorithms to describe the semantics of ECMAScript language constructs and ECMAScript language types. The specification types include Reference, List, Completion, Property Descriptor, Lexical Environment, Environment Record, and Data Block.
Specification type是为了便于描述ECMAScript language语义而引入的,包括7种,
Reference
,List
,Completion
,Property Descriptor
,Lexical Environment
,Environment Record
,Data Block
。
2. 原始值
4.3.2 primitive value
member of one of the types Undefined, Null, Boolean, Number, Symbol, or String.
原始值是以下类型的值,Undefined
,Null
,Boolean
,Number
,Symbol
,String
,
即所有ECMAScript language types中,只有Object
类型的值不是原始值。
这里值得注意的是,原始值与其对应的对象值是不同的,对象值的类型是Object
。
原始值 | 对象值 |
---|---|
true | new Boolean(true) |
1 | new Number(1) |
'x' | new String('x') |
甚至我们有,
console.assert(new Boolean(false));
此外,访问原始值的属性,ECMAScript会自动创建一个临时的与原始值对应的对象值(包装对象),
const a = 1;
a.b = 2; // 创建临时的new Number(1),随后再销毁
a.b; // undefined,又一次创建临时的new Number(1),随后再销毁
a; // 1
执行过程分析,
(1)a.b = 2;
,会创建一个临时对象temp1 = new Number(1);
,
给这个对象设置b
属性,temp1.b = 2;
,然后将临时对象temp1
丢弃。
(2)a.b;
,会又一次创建临时对象temp2 = new Number(1);
,
这个临时对象没有b
属性,读取temp2.b
的值为undefined
,最后temp2
也被丢弃。
(3)而a
的值,一直为原始值1
。
参考6.2.4.9 PutValue ( V, W ),以及7.3.2 GetV ( V, P ),
If the value is not an object, the property lookup is performed using a wrapper object appropriate for the type of the value.
3. typeof
值 | typeof |
---|---|
undefined | 'undefined' |
null | 'object' |
true | 'boolean' |
'x' | 'string' |
Symbol.for('a') | 'symbol' |
1 | 'number' |
{} | 'object' |
这里需要注意的是,typeof null === 'object'
,
其他值的判断,都会返回对应ECMAScript language type的类型名称(首字母小写)。
4. instanceof
对于Object
类型的值,可以通过instanceof
来判断相应的值是哪个对象的实例,例如,
// 正则表达式值是RegExp的实例
/a/ instanceof RegExp
V instanceof target
的判断规则如下,参考12.10.4 Runtime Semantics: InstanceofOperator ( V, target ),
(1)V instanceof target
首先会先用target[Symbol.hasInstance](V)
来判断,
(2)然后再判断,target.prototype
是否在V
的原型链上,即,V.__proto__.__proto__....===target.prototype
。
满足target[Symbol.hasInstance](V)
,但是不满足V.__proto__.__proto__....===target.prototype
的例子如下,
function f() {
return this;
}
const g = f.bind({ a: 1 });
const obj = new g;
此时g.prototype === undefined
,g
的prototype
不在任何对象的原型链上,
但是g[Symbol.hasInstance](obj)
为true
,因此,obj instanceof g
也为true
。
网友评论