JavaScript prototype原型


Note: The prototype chain is traversed only while retrieving properties.
If properties are set or deleted directly on the object, the prototype chain is not traversed.

查找属性的时候会在原型链上找。
如果属性只是在对象本身上,那么原型链就未被遍历,主要是不需要查找全链,一开始就找到了。


Most modern browsers, however, do offer property available called __proto__ (that's 2 underscores either side), 2个下划线。在下面划线 score 划线。

Most modern browsers, however, do offer property available called __proto__ (that's 2 underscores either side),
which contains the object's constructor's prototype object.
For example, try person1.__proto__ and person1.__proto__.__proto__ to see what the chain looks like in code!

Since ECMAScript 2015, you can access an object's prototype object indirectly via Object.getPrototypeOf(obj).

原型属性:继承的成员在哪被定义?

继承的属性和方法在哪定义?如果你找Object引用页面,你会看到listed在left hand side有大量的属性和方法,继承的成员比我们看到person1对象上的多。
一些被继承,一些没有? 为什么?

继承的是定义在prototype属性上的(sub-namespace) ,那些 Object.prototype.开头的,而不是Object.开头的。

prototype的属性的值是一个对象,是一个bucket用来存储可以被对象继承的属性和方法, further down the prototype chain.

Object.prototype.toString()
Object.prototype.valueOf()

看起来奇怪,你怎么能有一个方法定义在constructor,它本身就是个函数。
一个function也是个对象的一种, 看看Functions() 构造函数。 Function() constructor
(function(){}).constructor === Function     which returns true.

prototype is 属性,包含了对象,对象里面定义了成员,用来被继承。

再来看看create

let person2 = Object.create(person1)

person2使用person1的prototype对象创建。  person2.__proto__  就是 person1


构造函数的属性

每个构造函数都有原型属性。


阅读量: 404
发布于:
修改于: