我給一個屬性的功能,使用該功能作爲一個構造函數,像這樣:此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)
或原型屬性
hasPrototypeProperty(hinst,'high')
最後一行拋出一個異常說
TypeError: Cannot read property 'constructor' of undefined
我想我對財產有一些小姐的理解,怎麼可能'不願'訪問'高'財產?
'high'是構造函數的性質,因此:'hinst.constructor.high'應該返回' 「2.0」'。 'hinst.constructor.hasOwnProperty('high')'=>'true'。 – undefined