javascript - Object within Prototype -


this question has answer here:

i have created object within prototype , trying access variable constructor this, alert returning undefined.

constructor

function example() {     this.param1 = 'test'; } 

prototype

example.prototype = {     constructor: example,     obj: {         sample:function() {              alert(this.param1); // error undifined          }     } }; 

instantiate

var o = new example(); o.obj.sample(); 

any advice appreciated.

you can though

function example() {     this.param1 = 'test'; } example.prototype = {     constructor: example,     obj: {         sample:function(){             alert(this.param1); // error undifined         }     } };  var o = new example(); o.obj.sample.call(o); // <--- use "call" supply context. in case, context "o" // or o.obj.sample.bind(o)(); // or "bind" context, in case "o" 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo