事実 (クレジットは @IHateLazy に送られます):
aspenProto.constructor
ですMammal
。これは、constructor
実際には Mammal.prototype の属性であり、メソッドの作成時に設定されるためです。(プロパティを保持する) プロトタイプが によって上書きされたため、Canine.prototype.constructor
は ではありません。Canine
constructor
new Mammal()
テスト:
aspen.constructor = function Hello(){}; // aspen is Hello in Firebug,
// Dog in Chrome
aspen.constructor.name = "test" // still Hello in Firebug,
// name is also Hello in both
aspen.constructor = function(){}; // aspen is Object in Firebug
aspen.constructor.name = "test" // still Object in Firebug
aspen.constructor = null; // still Object and Dog
({constructor: function Hello(){}}) // Hello in Firebug AND Chrome
({constructor: function (){}}) // Object in both (not surprisingly)
({constructor:{name:"Hi"}}) // "Object" in FB, "Hi" in Chrome
x={constructor:function(){})
x.constructor.name="Hello" // x is Object in both
x=new Object()
x.constructor=function Hello(){} // x is Hello in both
new (function(){})() // Object in both
new (function(){
this.constructor=function Hello(){}
})() // Hello in both
結論:
constructor
Firebug は常にオブジェクトの独自のプロパティに依存して名前を付けます。constructor
が名前付き関数の場合、 constructor
name
(書き込み可能ではありません - @IHateLazy に感謝します) を使用します。constructor
プロパティが無名関数であるか、関数ではない場合、Firebug は代わり"Object"
に を使用します。
Chrome は、各オブジェクトの実際のコンストラクターを内部プロパティとして保持します。そのプロパティにアクセスできない (オブジェクトが構築されていない) 場合、または Object である場合にのみconstructor
、オブジェクトのプロパティを調べます。コンストラクターが名前付き関数の場合、内部に格納された名前を使用します。コンストラクターが関数ではない場合、または匿名の場合は、name
プロパティを使用します。