javascript 里 ~function(){} 是什么意思?

大家好~

[code="java"]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

산채탱크대전... 주탱크방향:WASD,SPACE 발사. 부탱크방향:우,아래,왼쪽,오른쪽.L 발사

~function(){

var mainTank; //주탱크
var subTank; //부탱크
var eTanks = []; //적탱크
var plays; //1,2플레어모식

var eTankLife = 20; //적탱크총수
var level = 1; //목전 관수
var eTanksLength = 3; //매번 나타날수 잇는 적탱크수량
var maps = [map1, map2, map3]; //지도
var map; //목전지도
var u; //시간제어기
var xs = [[0,12,22], [0, 8, 16, 22], [0, 6, 12, 18, 22] ];
var Gb = {
attr : function(){
if (arguments.length == 1){
return Gb[arguments[0]];
}else if (arguments.length == 2){
Gb[arguments[1]] = arguments[0]
return Gb;
}
}

}
[/code]

这里当中~function(){}是什么意思呢?
map.js 只是简单的声明地图模式的变量。
[code="java"]
var map1 = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,2,2,0,0,2,2,0,0,0,2,2,0,0,2,2,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,2,2,3,3,2,2,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,2,2,3,3,2,2,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,2,2,0,0,2,2,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,2,2,0,0,2,2,0,0,2,2,0,2,2,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,2,2,0,0,2,2,0,0,0,2,0,0,0,0,2,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,2,0,0,0,0,2,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,2,2,2,2,2,2,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,2,2,2,2,2,2,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,2,0,0,0,0,2,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,2,0,0,0,0,2,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,2,0,0,0,0,2,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,2,2,0,2,2,0],
[0,2,2,0,0,2,2,0,0,0,2,2,2,2,2,2,0,0,2,2,0,2,2,0],
[0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,2,2,7,7,2,2,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,2,2,7,7,2,2,0,0,0,0,0,0,0,0]
];
[/code]
在这里是使用了 jQuery,但是jQuery相关的没有找到,请求您的一臂之力, 谢谢!

2个回答

这个跟JE上的一个人写的坦克游戏里的代码一样嘛,不过说明怎么不是中文了,链接如下:
[url]http://www.iteye.com/topic/400663?page=1[/url]

这里当中~function(){}是什么意思呢?
作者观点:产生一个闭包.这份代码里主要是避免全局变量发生命名冲突. ~不是运算符. 在这里~换成任意符号或者void都可以. 意思是让后面的函数变成表达式去执行而不是声明.

其中一个观点认为:
[quote]"~"是按位非运算符,见犀角书88页。

楼主使用这种写法比较简化,目的还是为了执行匿名函数,等价于:
var p = ~function(){alert('hello')}();

至于返回值,可以使用以下代码体会一下:
alert(!function (){alert('hello');return false;}())
alert(~function (){alert('hello');return 1;}()) [/quote]

可能就是调用匿名函数的意思吧

也许是相当于

(function(){})();

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问