When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its?prototype. That prototype object has a prototype of its own, and so on until an object is reached with?null?as its prototype. By definition,?null?has no prototype, and acts as the final link in this?prototype chain.
Note:?Following the ECMAScript standard, the notation?someObject.[[Prototype]]?is used to designate the prototype of?someObject. The?[[Prototype]]?internal slot can be accessed with the?Object.getPrototypeOf()?and?Object.setPrototypeOf()?functions. This is equivalent to the JavaScript accessor?__proto__?which is non-standard but de-facto implemented by many JavaScript engines.
所有對(duì)象在創(chuàng)建時(shí)都有一個(gè)invisible property:__proto__,指向它的構(gòu)造函數(shù)的prototype對(duì)象。
例:
var obj = {name: 'jack'};
var arr = [1,2,3];
var date = new Date;
console.log(obj.__proto__ === Object.prototype); // true
console.log(arr.__proto__ === Array.prototype) ; // true
console.log(date.__proto__ === Date.prototype);? // true
自定義類創(chuàng)建的對(duì)象的原型指向其對(duì)應(yīng)的構(gòu)造函數(shù)的prototype對(duì)象
例:
function Person(name) {
? ? this.name = name
? }
var p = new Person('jack')
console.log(p.__proto__ === Person.prototype) // true
不使用new的話也會(huì)有prototype嗎神僵?
改變object的隱式原型(__proto__)會(huì)發(fā)生什么融求?