我去,JS自执行匿名函数竟然有20几种写法!

原创
2013/01/17 09:17
阅读数 1.4K
发现一个新的自执行写法:
{ var x; x=1; alert(x);}  //2013-01-30 14:45:57
//五大类,20几种写法

//第一类
//最常见的一种
( function(w) {
	alert(w.location.href+","+11);
}(window));

[ function(w) {
	alert(w.location.href+","+11);
}(window) ];


//第二类
~ function(w) {
	alert(w.location.href+","+11);
}(window);

! function(w) {
	alert(w.location.href+","+11);
}(window);

+ function(w) {
	alert(w.location.href+","+11);
}(window);

- function(w) {
	alert(w.location.href+","+11);
}(window);


//第三类
delete function(w) {
	alert(w.location.href+","+11);
}(window);

typeof function(w) {
	alert(w.location.href+","+11);
}(window);

void function(w) {
	alert(w.location.href+","+11);
}(window);

new function(w) {
	alert(w.location.href+","+11);
}(window);

new function() {
	alert(window.location.href+","+11);
}; 


//第四类
var f = function(w) {
	alert(w.location.href+","+11);
}(window);


//第五类
1, function() {
	alert(window.location.href+","+11);
}();

1 ^ function() {
	alert(window.location.href+","+11);
}();

1 > function() {
	alert(window.location.href+","+11);
}();

1 < function() {
	alert(window.location.href+","+11);
}();

1 / function() {
	alert(window.location.href+","+11);
}();

1 * function() {
	alert(window.location.href+","+11);
}();

1 | function() {
	alert(window.location.href+","+11);
}();

1 % function() {
	alert(window.location.href+","+11);
}();

1 & function() {
	alert(window.location.href+","+11);
}();
展开阅读全文
打赏
0
5 收藏
分享
加载中
更多评论
打赏
0 评论
5 收藏
0
分享
返回顶部
顶部