Javascript中定义函数的方式有多种,函数直接量就是其中一种。如var fun = function(){},这里function如果不赋值给fun那么它就是一个匿名函数。好,看看匿名函数的如何被调用。
(function(x,y){
alert(x+y);
return x+y;
}(3,4));
(function(x,y){
alert(x+y);
return x+y;
})(3,4);
这种方式也是很多库爱用的调用方式,如jQuery,Mootools。
void function(x) {
x = x-1;
alert(x);
}(9);
-function(x,y){
alert(x+y);
return x+y;
}(3,4);
+function(x,y){
alert(x+y);
return x+y;
}(3,4);
--function(x,y){
alert(x+y);
return x+y;
}(3,4);
++function(x,y){
alert(x+y);
return x+y;
}(3,4);
~function(x, y) {
alert(x+y);
return x+y;
}(3, 4);
[function(){
console.log(this) // 浏览器得控制台输出window
}(this)]
typeoftypeof function(){
console.log(this) // 浏览器得控制台输出window
}(this)
deletedelete function(){
console.log(this) // 浏览器得控制台输出window
}(this)
voidvoid function(){
console.log(this) // 浏览器得控制台输出window
}(this)
new方式,传参数new function(win){
console.log(win) // window
}(this)
new function(){
console.log(this) // 这里的this就不是window了
}
1, function(){
console.log(this) // window
}();
1^function(){
console.log(this) // window
}();
1>function(){
console.log(this) // window
}();
http://1.hehongwei44.sinaapp.com/?p=399
原文:http://www.cnblogs.com/happyty/p/4977186.html