Skip to content Skip to sidebar Skip to footer

Javascript Nested Dictionary Null Values

Below I have declared a 'class' in JS and am writing methods for it. The class represents a widget (more info class structure). The problem I'm having is that when I print 'this'

Solution 1:

The value of this inside your click handler is not the same as the value outside that handler. You need to stash this in a local variable and use that instead.

var statusObj = this;
this.onClick = function () {
    console.log('Clicked ' + statusObj.opts.meterName);
};

Alternatively, you could use .bind():

this.onClick = function () {
    console.log('Clicked ' + this.opts.meterName);
}.bind(this);

The value of this is always set on every individual call to a function based on the circumstances of the call.

Post a Comment for "Javascript Nested Dictionary Null Values"