var obj = {
"key": "some",
"foo": function() {return obj.key; // key undefined}
}
is there a way to get the reference to key inside the function?
Probably. If foo
is called as obj.foo()
, you'll be able to use this.key
. However, obj
should not be undefined
at that point; if you're changing it later and don't want to rely on this
, you can keep it in a different scope:
var obj = (function () {
var obj = {
key: "some",
foo: function () { return obj.key; }
};
return obj;
})();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.