prototypical inheritance javascript
Coming from a Java background, Javascript is a new world I'm trying to grasp.
Im kind of struggeling with how prototypical inheritance exactly works.
What i got from __proto__ VS. prototype in JavaScript and other sources was helpfull but I really want to make sure I grasp this topic. Are the following statements right?
__proto__
, a property of objects, is an object which represents the prototype of the object. This object can in turn also have an __proto__
property until the Object object, the end of the chain is reached.
prototype
is a property on a function object and is an object itself. When an object is instantiated from a function using the new
keyword, the __proto__
of that new instance will be the prototype
property of the constructor function. For instance:
let random = new Array();
console.log(random.__proto__); //logs the object which is the prototype of random
console.log(Array.prototype); //logs the same object as random.__proto__
console.log(random.__proto__.__proto__); // logs the Object prototype object
console.log(Object.prototype); // logs the same object as random.__proto__.__proto__
Also when the objects are tested with each other for equality they are the same object in the following code:
console.log(random.__proto__ === Array.prototype); // logs true
console.log(random.__proto__.__proto__ === Object.prototype ); // logs true
Since objects are tested for equality by reference does this mean that there is actually one instance of the Object.prototype
object and that all objects __proto__
refer to this instance?
Thanks in advance.
如果你的console.log(typeof(Ojbect))和console.log(typeof(Array))你有一个“函数”所有不是基本类型的(undefined,null ...)是一个对象或“一个实例“在JavaScript中,意味着函数从对象继承。
链接地址: http://www.djcxy.com/p/30064.html上一篇: 访问Javascript对象原型
下一篇: 原型继承javascript