0

質問: p1 のコンストラクターが Person になるのはなぜですか? Man ではありませんか?

function Person()
{
 this.type='person'
}
function Man()
{
 this.type='Man'
}

Man.prototype=new Person();

var p1=new Man();
console.log('p1\'s constructor is:'+p1.constructor);
console.log('p1 is instance of Man:'+(p1 instanceof Man));
console.log('p1 is instance of Person:'+(p1 instanceof Person));

http://jsfiddle.net/GSXVX/

4

1 に答える 1

0

元のオブジェクト.constructorを上書きしたときに元のプロパティを破棄しました...prototype

Man.prototype = new Person();

手動で置き換えることはできますが、(ES5 実装で) 列挙不可にしない限り、列挙可能なプロパティになります。

Man.prototype=new Person();
Man.prototype.constructor = Man; // This will be enumerable.

  // ES5 lets you make it non-enumerable
Man.prototype=new Person();
Object.defineProperty(Man.prototype, 'constructor', {
    value: Man,
    enumerable: false,
    configurable: true,
    writable: true
});
于 2012-06-01T04:07:30.750 に答える