2013-07-29 81 views
0

我知道有三種方法來獲取對象的原型,在遵循例如三個方法的結果是一樣的:混淆關於函數原型和object.getPrototypeOf

function Person(name) { 
     this.name = name; 
    } 

    Person.prototype.say = function() { 
     console.log("hello"); 
    } 

    var person = new Person(); 

    console.log(person.constructor.prototype); //Person {say: function} 
    console.log(Object.getPrototypeOf(person)); //Person {say: function} 
    console.log(person.__proto__); //Person {say: function} 

但是當檢查它通過創建一個對象Object.create,結果似乎不同:

var person = { 
     name: "Lee", 
     age: "12" 
    } 

    var per1 = Object.create(person); 

    console.log(per1.constructor.prototype) //Object {} 
    console.log(Object.getPrototypeOf(per1)) //Object {name: "Lee", age: "12"} 
    console.log(per1.__proto__) //Object {name: "Lee", age: "12"} 

不對象將按照其構造函數的原型?如何解釋上面的例子?

在這裏看到演示:http://jsfiddle.net/hh54188/A9SsM/

回答

0

getPrototypeof只是的__proto__實現,但他們都認爲是過時的或非標準的現在。至於你的第三個例子,我不相信你有任何輸出,因爲它應該是per1.prototype.constructor。如果你不是這個意思,我不能在構造函數的任何地方找到這個函數。