我知道有三种方法可以获取对象的原型,在下面的示例中,三种方法的结果是相同的:
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/