javascript: how to access static properties -


i want access static property using instance. this

function user(){     console.log('constructor: property1=' + this.constructor.property1) ; } user.prototype = {     test: function() {         console.log('test: property1=' + this.constructor.property1) ;     } }     user.property1 = 10 ;   // static property  var inst = new user() ; inst.test() ; 

here same code in jsfiddle

in situation don't know class instance belongs to, tried access static property using instance 'constructor' property, without success :( possible ?

so tried access static property using instance 'constructor' property

that's problem, instances don't have constructor property - you've overwritten whole .prototype object , default properties. instead, use

user.prototype.test = function() {     console.log('test: property1=' + this.constructor.property1) ; }; 

and might use user.property1 instead of detour via this.constructor. can't ensure instances on might want call method have constructor property pointing user - better access directly , explicitly.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -