if else太冗长了,有什么好方法可以优化下吗?自己想了好久没想出来。 Move:function(){
var p = arguments[0];
var = p.getAttribute("").split("=");
var i = parseInt([0]);
var j = parseInt([1]);
var direction,time,distance;
if(j+1<this.mapSizeX&&this.Map[i][j+1]=="no"){
direction="rightleft";
distance=this.imgWidth;
this.Map[i][j+1]=this.Map[i][j];
this.Map[i][j]="no";
j=j+1;
}
else if(j-1>=0&&this.Map[i][j-1]=="no"){
direction="rightleft";
distance=-this.imgWidth;
this.Map[i][j-1]=this.Map[i][j];
this.Map[i][j]="no";
j=j-1
}
else if(i+1<this.mapSizeY&&this.Map[i+1][j]=="no"){
direction="bottomtop";
distance=this.imgHeight;
this.Map[i+1][j]=this.Map[i][j];
this.Map[i][j]="no";
i=i+1
}
else if(i-1>=0&&this.Map[i-1][j]=="no"){
direction="bottomtop";
distance=-this.imgHeight;
this.Map[i-1][j]=this.Map[i][j];
this.Map[i][j]="no";
i=i-1
}
this.Change(p,direction,distance,i,j);
this.PrintMap();
},
var p = arguments[0];
var = p.getAttribute("").split("=");
var i = parseInt([0]);
var j = parseInt([1]);
var direction,time,distance;
if(j+1<this.mapSizeX&&this.Map[i][j+1]=="no"){
direction="rightleft";
distance=this.imgWidth;
this.Map[i][j+1]=this.Map[i][j];
this.Map[i][j]="no";
j=j+1;
}
else if(j-1>=0&&this.Map[i][j-1]=="no"){
direction="rightleft";
distance=-this.imgWidth;
this.Map[i][j-1]=this.Map[i][j];
this.Map[i][j]="no";
j=j-1
}
else if(i+1<this.mapSizeY&&this.Map[i+1][j]=="no"){
direction="bottomtop";
distance=this.imgHeight;
this.Map[i+1][j]=this.Map[i][j];
this.Map[i][j]="no";
i=i+1
}
else if(i-1>=0&&this.Map[i-1][j]=="no"){
direction="bottomtop";
distance=-this.imgHeight;
this.Map[i-1][j]=this.Map[i][j];
this.Map[i][j]="no";
i=i-1
}
this.Change(p,direction,distance,i,j);
this.PrintMap();
},
解决方案 »
- jQuery控制embed元素,remove以后在页面上显示一个黑框
- 正则表达 匹配任意字符串 但不匹配 www 字符串
- JS脚本引用问题
- 在google浏览器中用js实现添加到收藏夹的功能呢?
- 开心网标题区的文字闪烁和闪烁的小信封怎么实现的?
- 可以这样直接设定自定义属性吗?
- 怎样在关闭页面后释放该页面使用的ocx资源呀!!
- ■■请教:页面中用javascript如何控制整个TABLE的字体、是否只读readonly等等属性呢■■
- 如何去掉window.close();而带来的“你查看的网页正在试图关闭窗口,是否关闭此窗口?”
- 获取和修改页面json对象属性的值
- javascript写类方法有多少种?
- 问大家一个问题,有些网站js压缩成了一条语句,firebug无法单步进行调式,如何调试呢
将这个if ........else
修改成
switch(变量)
{
case 值1:
{
}
case 值2:
{
}
default:
{
}
}
distance=-this.imgHeight;
this.Map[i-1][j]=this.Map[i][j];
this.Map[i][j]="no";
i=i-1
其实也无需优化 不会存在什么性能问题 也没有什么循环的东西在里边 无非就是给人感觉代码相似度有些高但是有一点 虽然这里this我不知道楼主具体代码中指代什么 但是可以把分支里的this.什么什么都提出来作为一个局部变量 以减少每次判断条件中对属性的查找时间 如果你的代码确实有性能问题 那楼主不妨一试
,bind:function (td){
if(td.className=='p0')return;/* 如果点击的是空白网格,则忽视 */
function V(x){return x>-1&&x<3};
var x=td.cellIndex,y=td.parentNode.rowIndex,$=this.map.rows
,$$=[
V(y-1)&&$[y-1].cells[x]
,V(x+1)&&$[y].cells[x+1]
,V(y+1)&&$[y+1].cells[x]
,V(x-1)&&$[y].cells[x-1]
];
for (var i=0;i<$$.length;i++ )/* 测试是否可以移动网格 */
if($$[i]&&$$[i].className=='p0')
return this.move(td,$$[i]);
}
上面这段代码
if($$[i]&&$$[i].className=='p0')
这里不是很明白。为什么$$[i]可以获取className?
V(y-1)&&$[y-1].cells[x]
这种不是返回true或false吗?
在逻辑运算中,并不会把一些非bool类型的参数强制转换为bool类型
在看下面的alert(true&&"aa");//aa
alert(false&&"aa");//false
alert(true||"aa");//true
alert(false||"aa");//aa