在JavaScript中,每一個對象會有一個原型對象。對象會從原型對象繼承一些屬性和方法。javascript
在JavaScript中,訪問一個對象的屬性時,JS引擎會首先在該對象自身上線查找該屬性。若是找到了,則直接返回該屬性的值;若是沒有找到,則會去改對象的原型上面繼續查找。若是對象的原型上面也沒有這個屬性,則繼續在原型對象的上原型上面查找,如此一級級繼續往上,直到原型爲null,此時JS引擎返回該對象的屬性值爲undefined。java
/** * two methods to implement inheritance; */ function Base(type){ this.type = type; } Base.prototype.base=function(){ console.log(`${this.type} is in base func`); } // method one function Sub(type){ this.type = type; } Sub.prototype = Object.create(new Base('base')); Sub.prototype.sub=function(){ console.log(`${this.type} is in sub func`); } // method two function Foo(type){ this.type = type; } Object.setPrototypeOf( Foo.prototype, new Sub('sub')); Foo.prototype.foo=function(){ console.log(`${this.type} is in foo func`); } let sub = new Sub('sub1'); sub.base(); sub.sub(); sub instanceof Sub; // true sub instanceof Base; // true let foo = new Foo('foo1'); foo.base(); foo.sub(); foo.foo(); foo instanceof Foo; // true foo instanceof Sub; // true foo instanceof Base; // true
預測下面幾個表達式的結果函數
Object.getPrototypeOf(function(){}).constructor === Function; Object.getPrototypeOf(Function).constructor === Function; Object.getPrototypeOf(Object.getPrototypeOf(Function)).constructor === Object;
答案:this
true;
let obj = Object.create(null); console.log(obj);
這篇medium上得到8.6K讚的文章Prototypes in JavaScript講得很清楚了。spa