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/