如果您所提供的代碼完成(用的是什麼兩個$(document).ready(function() {});
語句中除外),比這個代碼什麼都不做,並且這個函數永遠不會執行。無論是否包裝圓括號都是一樣的。
通過在括號中包裝功能,您可以創建一個anonymousfunction。但是,函數必須立即執行,或者存儲在一個變量中(這會取消匿名部分)。您經常會看到這種技術,以避免使用臨時變量或僅用於初始化較大應用程序的變量來污染全局範圍。例如。
(function() {
// Do initialization shtuff
var someLocalVariable = 'value';
})();
// Notice the `();` here after the closing parenthesis.
// This executes the anonymous function.
// This will cause an error since `someLocalVariable` is not
// available in this scope
console.log(someLocalVariable);
那麼,你的代碼缺少的是在函數結束時,右括號後的();
。這裏是你的代碼(大概)看起來像:
(function() {
var someobj = window.someobj = [];
var parentId = '#wrapper';
$(document).ready(function() {
//some code here
});
$(document).ready(function() {
//some code here
});
})();
你不向我們展示足夠的代碼。請帶我們通過至少與首位左括號相符的右括號。到目前爲止我們知道的是我們有一個匿名函數被定義。代碼(function(){})();通常用於設置某些東西而不污染全局名稱空間。 – Nosredna 2009-11-01 20:17:32