[英]Get all keys of Object in this when we use one of the key as self invoking function in Node.js /Js
如何使用此關鍵字從自調用函數調用其他鍵。目前,我正在這樣做。
module.exports = function (url_obj) {
return {
const: (function () {
//unable to get add function also when i use this keyword
##console.log(this);##
this.value_main=33;//unable to change its value as i am not getting value_main key in "this"
})(),
add : function () {},
value_main:12
}
}
解決問題的一種方法是將對象存儲在var中,並將其作為自變量傳遞給自調用函數:
var myObj = {
add : function () {},
value_main:12
};
myObj.const_val = (function(self){
self.value_main = 13
//self.otherkey...
//self.add();
console.log(self);
return;// return value if you want const_val to have one
})(myObj);
return myObj;
const
是javascript(ES6)中的關鍵字,因此您不能將其用作鍵。 如果確實要這樣做,可以使用{"const": ...}
,但這不是一個好主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.