Typically, calling this
would work; but I am calling the method in another context, so this
refers to that context rather than the 'parent' object.
Here's the code:
var someConfig = {
things: [
{
attr1: 'foo',
attr2: 'bar',
action: function() {
console.log(this);
}
}
]
}
function Constructor(config) {
var self = this;
self.name = 'test';
self.things = config.things;
$.each(self.things, function(i, thing) {
thing.action();
});
}
var obj = new Constructor(someConfig);
console.log(obj);
Here's a jsfiddle. The goal is to have both objects that are in the console to be the same, but this
in the context of the action
method returns the original object to which action
belongs, rather than the constructor.
The only thing I can think of is to pass self
into action
, but I think there's a better way.