I have the following JS code:
var Item = function ()
{
this.property = '';
this.myfunction = function ()
{
var value = this.property;
};
};
however, this does not point to the defining class so value doesn’t get anything.
how do I access this.property from inside my function?
You need to create a closure which captures the value of parent scope’s
this:Update: As others have pointed out, this closure is not needed when
Itemis used as a constructor (new Item()). Noting it here for future reference.