オブジェクトのプロトタイプを取得する方法は 3 つあります。次の例では、3 つの方法の結果は同じです。
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/