为什么 (this) & obj 在 function 之后使用?
Why is (this) & obj used after function?
(function(obj){
$.each(Templates.inputs, function(key, value){
obj.elementsKey.push(key);
obj.elementsValueType.push(value.type);
});
}(this));
why is this (this)) used after function?
And What does obj does when used as parameter
这就是所谓的 IIFE - 您声明一个函数并立即调用它。
此函数有参数 obj
,其中放置了当前变量 this
。因此,例如,如果你在全局范围内 运行 这个 IIFE,你将在函数内的 obj
变量中得到一个 window
对象。
(function(obj){
$.each(Templates.inputs, function(key, value){
obj.elementsKey.push(key);
obj.elementsValueType.push(value.type);
});
}(this));
why is this (this)) used after function? And What does obj does when used as parameter
这就是所谓的 IIFE - 您声明一个函数并立即调用它。
此函数有参数 obj
,其中放置了当前变量 this
。因此,例如,如果你在全局范围内 运行 这个 IIFE,你将在函数内的 obj
变量中得到一个 window
对象。