var myObj = (function () {
// 做你想做的事
})();
但是,上述的方式若使用 JSLint 去檢查,都會出現這一個錯誤訊息「jslint:Move the invocation into the parens that contain the function.」
以往,我都會忽略這個錯誤訊息,而今找到了一個解法,只要將上述的 code 改成下方的方式就可以解決這個錯誤:
var myObj = (function () {
// 做你想做的事
}());
小小的變更一下第三行括號的位置即可。其實,括號的位置並不影響程式的運作,在這個網頁裡有個相當好的說明: http://stackoverflow.com/questions/4979252/jslint-error-move-the-invocation-into-the-parens-that-contain-the-function
To pass JSLint's criteria, it needs to be written like this:
}(jQuery));
Though I think that particular criteria is a bit subjective. Both ways seem fine in my opinion.
(function () {})()
makes a bit more sense to me since you wrap the full function, then call it(function () {}())
looks like you're wrapping the result of the function call in a parens ...
沒有留言:
張貼留言