javascript - Object within Prototype -
this question has answer here:
- nested object literal access parent 3 answers
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"