lundi 9 mars 2015

How to tell the difference between undeclared and undefined JavaScript object properties?

So is there no way to check if an object property has been formally declared vs not?? For example...



var obj={};

console.log( non_existent_variable ) //throws not defined error
console.log( obj.non_existent_property) //no error,===undefined


Consider the following...



function m(){
this.prop;
}
var MyObj=new m();


In this case, I think there should be a knowable difference between MyObj.prop which has been formally declared, and MyObj.non_existent_property which has not.


Unfortunately they both ===undefined, both return false with hasOwnProperty and neither are enumerated in a for( in ) loop. Anything I'm missing?


(IMHO, why couldn't the parser set declared but unset properties to null? What would be the harm in that?)





Aucun commentaire:

Enregistrer un commentaire