阅读指南:
1.前面的标题号按照规范中的标题号书写的,所以不是从1开始。
2.大概顺序是【标准原文】->【翻译】->【我的理解】
3.一般原文是【黑色原生】字体,翻译是【红色】字体
4.大部分是原型相关
5.[[prototype]]就是我们平常说的__proto__
6.翻译大多是谷歌+我自己翻译的
4 Overview
概述
4.3 Terms and definitions
术语和定义
4.3.4 constructor
构造函数
function object that creates and initializes objects
NOTE The value of a constructor’s prototype property is a prototype object that is used to implement inheritance and shared properties.
创建和初始化对象的函数对象
注意:构造函数的 prototype 属性的值是一个原型对象,
用于实现继承和共享属性。
4.3.5 prototype
object that provides shared properties for other objects
NOTE When a constructor creates an object, that object implicitly references the constructor’s prototype property for the purpose of resolving property references.
为其他对象提供共享属性的对象
注意:
当构造函数创建一个对象的时候,这个对象会隐式引用这个构造函数的prototype属性中的属性,以便于解析属性的引用(意思可能是:通过构造函数创建的对象可以调用构造函数上的方法,就是原型链,本对象的方法中找不到对象方法,就向上查找原型中的方法,对象的原型(__proto__)又指向构造函数的原型对象(prototype)。)
The constructor’s prototype property can be referenced by the program expression constructor.prototype, and properties added to an object’s prototype are shared, through inheritance, by all objects sharing the prototype.
Alternatively, a new object may be created with an explicitly specified prototype by using the Object.create built-in function.
构造函数的原型属性可以被程序表达式constructor.prototype引用,并且添加到对象原型的属性通过继承被共享原型的所有对象共享。
或者,可以使用 Object.create 内置函数使用明确指定的原型创建新对象。
如果用C表示构造函数,O表示通过构造函数创建的对象:
let O = new C
O.constructor.prototype === C.prototype
9 Ordinary and Exotic Objects Behaviours
普通对象和Exotic 对象的行为
9.1 Ordinary Object Internal Methods and Internal Slots
普通对象内部方法和内部槽
(这个内部槽,可能就是属性,一般是不能获取的,是内部实现的)
All ordinary objects have an internal slot called [[Prototype]]. The value of this internal slot is either null or an object and is used for implementing inheritance. Data properties of the [[Prototype]] object are inherited (are visible as properties of the child object) for the purposes of get access, but not for set access. Accessor properties are inherited for both get access and set access.
9.1普通对象内部方法和内部槽 所有普通对象都有一个称为 [[Prototype]] 的内部插槽。
这个内部槽的值要么是空,要么是一个对象,用于实现继承。
[[Prototype]] 对象的数据属性被继承(作为子对象的属性可见)用于获取访问,但不用于设置访问。
获取访问和设置访问都继承了访问器属性。
Every ordinary object has a Boolean-valued [[Extensible]] internal slot that controls whether or not properties may be added to the object. If the value of the [[Extensible]] internal slot is false then additional properties may not be added to the object. In addition, if [[Extensible]] is false the value of the [[Prototype]] internal slot of the object may not be modified. Once the value of an object’s [[Extensible]] internal slot has been set to false it may not be subsequently changed to true.
每个普通对象都有一个布尔值 [[Extensible]] 内部槽,用于控制是否可以向对象添加属性。
如果 [[Extensible]] 内部插槽的值为 false,则可能不会向对象添加其他属性。
此外,如果 [[Extensible]] 为 false,则对象的 [[Prototype]] 内部槽的值可能不会被修改。
一旦对象的 [[Extensible]] 内部槽的值被设置为 false,它可能不会随后更改为 true。
上面这段没什么需要理解解释的。
17 ECMAScript Standard Built-in Objects
ECMAScript 标准内置对象
Unless otherwise specified every built-in function and every built-in constructor has the Function prototype object, which is the initial value of the expression Function.prototype (19.2.3), as the value of its [[Prototype]] internal slot.
除非另有说明,每个内置函数和每个内置构造函数都有 Function 原型对象,它是表达式 Function.prototype (19.2.3) 的初始值,作为其 [[Prototype]] 内部槽的值。
Object是一个构造函数,new Object出来的是一个对象
内置函数除了Object还有Boolean、String等等
我所理解的:每个内置函数、内置构造函数都可以理解成通过new Function生成的,比如let toString = new Function这样。
于是这些内置函数、内置构造函数,都能继承Function的原型对象peototype里面的方法。
这句话的重点我觉得还是在于:内置函数如果没有特殊说明,都是通过new Function生成的。
并且有fun.__proto__ === Function.prototype
Unless otherwise specified every built-in prototype object has the Object prototype object, which is the initial value of the expression Object.prototype (19.1.3), as the value of its [[Prototype]] internal slot, except the Object prototype object itself.
除非另有说明,否则每个内置原型对象都有 Object 原型对象,它是表达式 Object.prototype (19.1.3) 的初始值,作为其 [[Prototype]] 内部槽的值,除了 Object 原型对象本身。
这段跟上面函数的意思差不多。
讲一下这句【 except the Object prototype object itself. 除了 Object 原型对象本身】,可能是因为,Object原型对象是由引擎(或者标准?)创造的,【JS万物皆对象】,可能指的就是,除了对象本身,其他的都是又对象创造出来的。
所以每个对象的原型对象,都指向Object.prototype,
即:obj.prototype.__proto__ === Object.prototype
18 The Global Object
全局对象
The unique global object is created before control enters any execution context.
The global object does not have a [[Construct]] internal method; it is not possible to use the global object as a constructor with the new operator.
The global object does not have a [[Call]] internal method; it is not possible to invoke the global object as a function.
The value of the [[Prototype]] internal slot of the global object is implementation-dependent.
In addition to the properties defined in this specification the global object may have additional host defined properties. This may include a property whose value is the global object itself; for example, in the HTML document object model the window property of the global object is the global object itself.
全局对象
在控制进入任何执行上下文之前创建唯一的全局对象。
全局对象没有 [[Construct]] 内部方法; 不能将全局对象用作带有 new 运算符的构造函数。
全局对象没有 [[Call]] 内部方法; 不能将全局对象作为函数调用。
全局对象的 [[Prototype]] 内部槽的值是依赖于实现的。
除了本规范中定义的属性之外,全局对象可能还有其他主机定义的属性。
这可能包括一个属性,其值为全局对象本身;
例如,在 HTML 文档对象模型中,全局对象的 window 属性就是全局对象本身。
关于[[Construct]]和[[Call]]多说两句,函数对象实现这两个内部方法,
其中[[Call]]管函数调用,[[Construct]]管能不能通过new创建对象,即掌管函数是否是构造函数(构造函数才能通过new调用)。
箭头函数没有实现[[Construct]]内部方法,这也是它不能通过new调用的原因。
再说全局对象的[[prototype]]实现,(我的理解是)依托于引擎的构建实现,就像上面的举例,浏览器下全局对象是window,Node下是global。
大家可以自行控制台window.__proto__
19Fundamental Objects
基本对象
19.1Object Objects
Object对象
19.1.2 Properties of the Object Constructor
对象构造函数的属性
The value of the [[Prototype]] internal slot of the Object constructor is the intrinsic object %FunctionPrototype%.
Object.__proto__ === Function.prototype
19.1.3 Properties of the Object Prototype Object
Object.prototype对象的属性
The value of the [[Prototype]] internal slot of the Object prototype object is null and the initial value of the [[Extensible]] internal slot is true.
Object.prototype.__proto__ === null
[[Extensible]] :Object.prototype 可扩展,可以添加属性
19.1.3.1 Object.prototype.constructor
The initial value of Object.prototype.constructor is the intrinsic object %Object%.
Object.prototype.constructor === Object
19.2 Function Objects
Function 对象
19.2.2 Properties of the Function Constructor
构造函数的属性
The Function constructor is itself a built-in function object. The value of the [[Prototype]] internal slot of the Function constructor is %FunctionPrototype%, the intrinsic Function prototype object (19.2.3).
Function 构造函数本身就是一个内置的函数对象。
Function 构造函数的 [[Prototype]] 内部槽的值是 %FunctionPrototype%,即内部 Function 原型对象 (19.2.3)。
最后一句话指的是
Function.__proto__ === Function.prototype
19.2.3 Properties of the Function Prototype Object
Function.prototype的属性
The value of the [[Prototype]] internal slot of the Function prototype object is the intrinsic object %ObjectPrototype% (19.1.3).
Function.prototype === Function.__proto__
19.2.4 Function Instances
函数实例
Every function instance is an ECMAScript function object and has the internal slots listed in Table 27. Function instances created using the Function.prototype.bind method (19.2.3.2) have the internal slots listed in Table 28.
The Function instances have the following properties:
每个函数实例都是一个 ECMAScript 函数对象,并具有表 27 中列出的内部槽。
使用 Function.prototype.bind 方法 (19.2.3.2) 创建的函数实例具有表 28 中列出的内部槽。
Function 实例具有以下属性:
前两个属性,一个是name一个是length,暂时没什么可写的,直接上第三个,如下:
19.2.4.3 prototype
Function instances that can be used as a constructor have a prototype property. Whenever such a function instance is created another ordinary object is also created and is the initial value of the function’s prototype property. Unless otherwise specified, the value of the prototype property is used to initialize the [[Prototype]] internal slot of the object created when that function is invoked as a constructor.
NOTE Function objects created using Function.prototype.bind, or by evaluating a MethodDefinition (that are not a GeneratorMethod) or an ArrowFunction grammar production do not have a prototype property.
可用作构造函数的函数实例具有原型属性。
每当创建这样的函数实例时,也会创建另一个普通对象,该对象是函数原型属性的初始值。
除非另有说明,否则原型属性的值用于初始化该函数作为构造函数调用时创建的对象的 [[Prototype]] 内部槽。
使用 Function.prototype.bind 或通过计算 MethodDefinition(不是 GeneratorMethod)或 ArrowFunction 语法产生式创建的 NOTEFunction 对象没有原型属性。
我的理解:
假设构造函数为C,通过构造函数创建的对象为O
let C = function(){ this.name = 'cName' }
let O = new C
构造函数C具有原型属性,即C.prototype,当C这个构造函数被创建的时候,c.prototype也被创建出来。
如果没有特殊说明\
O.__proto__ === C.prototype
最后关于bind创建对象的说明,我还没有用到,不太了解,放在这里,留作后用。
19.3Boolean Objects
Boolean对象
19.3.2 Properties of the Boolean Constructor
构造函数boolean的属性
The value of the [[Prototype]] internal slot of the Boolean constructor is the intrinsic object %FunctionPrototype% (19.2.3).
Boolean 构造函数的 [[Prototype]] 内部槽的值是内部对象 %FunctionPrototype% (19.2.3)。
Boolean.__proto__ === Function.prototype
19.3.2.1 Boolean.prototype
The initial value of Boolean.prototype is the intrinsic object %BooleanPrototype% (19.3.3).
Boolean.prototype 的初始值是内在对象 %BooleanPrototype% (19.3.3)。
讲实话这句话看起来像一句废话,在我看来这句意思是:
Boolean.prototype === Boolean.prototype
19.3.3 Properties of the Boolean Prototype Object
Boolean.prototype的属性们
The value of the [[Prototype]] internal slot of the Boolean prototype object is the intrinsic object %ObjectPrototype% (19.1.3).
布尔原型对象的 [[Prototype]] 内部槽的值是内部对象 %ObjectPrototype% (19.1.3)。
即:
Boolean.prototype.__proto__ === Object.prototype
19.4Symbol Objects
Symbol对象
19.4.2 Properties of the Symbol Constructor
The value of the [[Prototype]] internal slot of the Symbol constructor is the intrinsic object %FunctionPrototype% (19.2.3).
Symbol 构造函数的 [[Prototype]] 内部槽的值是内部对象 %FunctionPrototype% (19.2.3)。
即:Symbol.__proto__ === Function.prototype
19.4.3 Properties of the Symbol Prototype Object
The Symbol prototype object is the intrinsic object %SymbolPrototype%. The Symbol prototype object is an ordinary object. It is not a Symbol instance and does not have a [[SymbolData]] internal slot.
The value of the [[Prototype]] internal slot of the Symbol prototype object is the intrinsic object %ObjectPrototype% (19.1.3).
19.4.3 Symbol原型对象的属性
Symbol 原型对象是内在对象 %SymbolPrototype%。
Symbol 原型对象是一个普通对象。 它不是 Symbol 实例,也没有 [[SymbolData]] 内部插槽。
Symbol 原型对象的 [[Prototype]] 内部槽的值是内部对象 %ObjectPrototype% (19.1.3)。
即:Symbol.prototype.__proto__ === Object.prototype
划到文档最后
中间各种对象掠过不说,都差不多。
19.2.3.2: In ECMAScript 2015, the [[Prototype]] internal slot of a bound function is set to the [[GetPrototypeOf]] value of its target function. In the previous edition, [[Prototype]] was always set to %FunctionPrototype%.
19.2.3.2:在 ECMAScript 2015 中,绑定函数的 [[Prototype]] 内部槽设置为其目标函数的 [[GetPrototypeOf]] 值。
在之前的版本中,[[Prototype]] 总是设置为 %FunctionPrototype%。
19.5.6.2: In ECMAScript 2015, the [[Prototype]] internal slot of a NativeError constructor is the Error constructor. In previous editions it was the Function prototype object.
19.5.6.2:在 ECMAScript 2015 中,NativeError 构造函数的 [[Prototype]] 内部槽是 Error 构造函数。
在以前的版本中,它是 Function 原型对象。
我
看了不少文章,还是没有那种通透的感觉,看别人的文章总会被误导,这次八一八规范里的东西。
语言就像一个工具,规范就是使用说明书。
但是这个使用说明我看完依旧有点云里雾里。
再捋一捋思绪,如果有所感,会再写一篇文章纪念。
希望对大家有所帮助。