if I have this json structure:
var j = {
param1: 'hello',
param2: 'world',
func: function() {
console.log(this.param1 + ' ' + this.param2);
}
};
this in func is undefined. How to access self in this json object? Thanks
EDIT:
I’m trying it with:
j.func();
thisis determined by how a function is called. To answer your question, we would need to see how you are calledfunc().If you call:
then,
thisinside offuncwill be set toj.If you call
func()directly (which is what happens if you passj.funcas a callback which is then called directly by some other function), thenthiswill probably be set to eitherwindoworundefineddepending upon whether you are in strict mode or not. For example:thiscan also be set explicitly by the caller by usingj.func.apply()orj.func.call()which allow the caller to specify the desired value ofthis.