2016-08-12 2 views
-1

私は関数の性質を与え、このように、コンストラクタとしてこの機能を使用するには:非常に奇妙な、私のテストでは、「高」は、インスタンスのプロパティでもない このjavascriptプロパティはインスタンスプロパティか、プロトタイププロパティですか?

function h(){this.a='abc';} 
h.high='2.0'; 
var hinst=new h(); 
function hasPrototypeProperty(object, name){ 
    return !object.hasOwnProperty(name) && (name in object); 
} 
console.log(h.hasOwnProperty('high')); //true 
console.log(hinst.hasOwnProperty('high'));//false 
console.log(hasPrototypeProperty(hinst, 'high'));//false 
console.log('a' in hinst); //true 
console.log('high' in hinst);//false 
console.log(hinst.prototype.constructor.high); //exception 

ある

hinst.hasOwnProperty)

またはprototypeプロパティ

hasPrototypeProperty(hinst,'high')

そして、最後の行は

を言って例外をスローします210

TypeError: Cannot read property 'constructor' of undefined

私は不動産に関する理解が不足していると思われますが、どのように 'hinst'が 'high'プロパティを訪問することができますか?

+2

'high'コンストラクタの特性であるので、' hinst.constructor.high'は '' "2.0" を返すべきです。 'hinst.constructor.hasOwnProperty( 'high')' => 'true'です。 – undefined

答えて

1

少し物事を壊しましょう。

ここでは、コンストラクタ関数が作成されます。これらは、newオペレータと共に使用するためのものです。広範な慣行は、その意図を明らかにするために最初の文字を大文字にすることです。

function H(){ this.a='abc'; } 

コンストラクタ関数がnewで呼び出された場合、これに似た何かが起こる:

(function(){ 
    var o = Object.create(H.prototype); 
    H.apply(o, arguments); 
    return o; 
}()); 

あなたは基本的にH.prototypeオブジェクトから継承する新しいオブジェクト({ a: 'abc' })で終わります。つまり、内部の[[Prototype]]プロパティを指しています。

H.prototypeは最初は単一のプロパティ(constructorはコンストラクタ関数Hを指します)を持つオブジェクトですが、自由に置換または拡張できます。あなたはおそらく、この行でやりたいことはどのです:

H.high='2.0'; 

しかし、その代わりに、あなたはコンストラクタ関数H(機能があまりにもオブジェクトである)にプロパティを追加しました。

console.log(H.hasOwnProperty('high'));    //true 
console.log((new H()).hasOwnProperty('high'));  //false 
console.log((new H()).hasPrototypeProperty('high')); //false 

例を修正しました。

function H(){ this.a='abc'; } 
 
H.prototype.high='2.0'; 
 
var hinst = new H(); 
 
function hasPrototypeProperty(object, name){ 
 
    return !object.hasOwnProperty(name) && (name in object); 
 
} 
 
console.log(H.hasOwnProperty('high'));   //false 
 
console.log(hinst.hasOwnProperty('high'));  //false 
 
console.log(H.prototype.hasOwnProperty('high')); //true 
 
console.log(hasPrototypeProperty(hinst, 'high')); //true 
 
console.log('a' in hinst);      //true 
 
console.log('high' in hinst);      //true 
 
console.log(H.prototype.high);     //2.0 
 
console.log(hinst.high);       //2.0


Inheritance and the prototype chain at MDN

1

ここでhfunctionのオブジェクトで、propertyという名前のhighが割り当てられています。したがって、インスタンスやプロトタイプには関係しません。

+0

まだちょっと混乱していますが、どうすれば "hinst"の "high"プロパティを訪問できますか? – Troskyvs

+0

@Troskyvs: 'h.high'ではなく' h.prototype.high'に入れてください!インスタンスは、コンストラクタからではなく、プロトタイプから継承します。 – Bergi

0

あなたのコンストラクタはプロトタイプチェーンの一部ではないので、コンストラクタとプロトタイプ

console.log(inst.prototype.constructor.high); // exception 
console.log(inst.constructor.high); // '2.0' 

の間、あなたのコード内のいくつかの混乱があります。
あなたが(ちょうど 機能です)コンストラクタ上の事実の後にプロパティを定義するとき、あなたはこの

function h() { 
this.a='abc'; 
} 
h.high='2.0'; 
console.log(h); 
// => { [Function: h] high: '2.0' } 

フランケンシュタイン機能オブジェクトのモンスターで終わります。

関連する問題