I am trying reference the parent object. This works fine in normal functions/methods, but in the closure/ self-invoking function, the parent object is undefined. How does the scoping work in this case? How can we reference the parent obj?
var user = {
name: "Some user",
methods: {
getUserName: function() {
return user.name
},
GetUserName: function() { }
}
f_name: (function () {
var fname = user.name; // **Error: user is undefined**
fname = fname.split(" ")[0];
console.log(fname);
return fname
}())
}
user.methods.getUserName(); // "Some user"
user.f_name // should log "Some" to the console;