(function(){var
// Will speed up references to window, and allows munging its name.
window = this,
// Will speed up references to undefined, and allows munging its name.
undefined,
// Map over jQuery in case of overwrite
_jQuery = window.jQuery,
// Map over the $ in case of overwrite
_$ = window.$, jQuery = window.jQuery = window.$ = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context );
}, // A simple way to check for HTML strings or ID strings
// (both of which we optimize for)
quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/,
// Is it a simple selector
isSimple = /^.[^:#\[\.,]*$/;jQuery.fn = jQuery.prototype = {
init: function( selector, context ) {
// Make sure that a selection was provided
selector = selector || document;
}这个函数为什么没有名字,怎么调用它呢
// Will speed up references to window, and allows munging its name.
window = this,
// Will speed up references to undefined, and allows munging its name.
undefined,
// Map over jQuery in case of overwrite
_jQuery = window.jQuery,
// Map over the $ in case of overwrite
_$ = window.$, jQuery = window.jQuery = window.$ = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context );
}, // A simple way to check for HTML strings or ID strings
// (both of which we optimize for)
quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/,
// Is it a simple selector
isSimple = /^.[^:#\[\.,]*$/;jQuery.fn = jQuery.prototype = {
init: function( selector, context ) {
// Make sure that a selection was provided
selector = selector || document;
}这个函数为什么没有名字,怎么调用它呢
alert("Hello");
};
sayHello();
还有上面的这种情况,将匿名函数赋给一个变量,然后通过变量调用。
(function(id){
alert(id);//结果为 2
})(2);
(function(id){
alert(id);//结果为 2
})(2);
用途2:(旧浏览器中不存在undefined关键字,用这种方法定义)
(function(undefined){
alert(undefined);
})();用途3:(变量作用域, 用来防止导入不同js文件中的变量重名)
(function(undefined){
var a = 1;
})();
alert(a);// undefined
他就是靠外面的这个括号运行的。
()在js里的function里就表示执行。
形如(function(){
/do something here*/
})();等价于匿名函数=function(){
/do something here*/
};//定义
匿名函数();//调用“匿名函数”可以换成任何你要的函数名字,这样写就很熟悉了,你会发现很多时候其实就在使用它。
// ...
};然后,再通过变量名调用
theFunction();
-------------------------------也可以定义完,马上一次性调用
(function() {
// ...
})();