开发者最容易犯的13个JavaScript错误(1)(2)
7. 区别整数数字和浮点数字 Differentiate float numbers from integer numbers
举例:
- var myNumber = 3.5;
- var myResult = 3.5 + 1.0; //We use .0 to keep the result as float
在JavaScript中,浮点与整数间没有区别。事实上,JavaScript中的每个数字都表示使用双精度64位格式IEEE 754。简单理解,所有数字都是浮点。
解决办法:不要使用小数(decimals),转换数字(numbers)到浮点(floats)。
- var myNumber = 3.5;
- var myResult = 3.5 + 1; //Result is 4.5, as expected
8. with()作为快捷方式的用法 Usage of with() as a shortcut
举例:
- team.attackers.myWarrior = { attack: 1, speed: 3, magic: 5};
- with (team.attackers.myWarrior){
- console.log ( “Your warrior power is ” + (attack * speed));
- }
讨论with()之前,要明白JavaScript contexts 如何工作的。每个函数都有一个执行 context(语句),简单来说,包括函数可以访问的所有的变量。因此 context 包含 arguments 和定义变量。
with() 真正是做什么?是插入对象到 context 链,它在当前 context 和父级 context间植入。就像你看到的with()的快捷方式会非常慢。
解决办法:不要使用with() for shortcuts,仅for context injection,如果你确实需要时。
- team.attackers.myWarrior = { attack: 1, speed: 3, magic: 5};
- var sc = team.attackers.myWarrior;
- console.log(“Your warrior power is ” + (sc.attack * sc.speed));
9.setTimeout/setInterval 字符串的用法 Usage of strings with setTimeout/setInterval
举例:
- function log1() { console.log(document.location); }
- function log2(arg) { console.log(arg); }
- var myValue = “test”;
- setTimeout(“log1()”, 100);
- setTimeout(“log2(” + myValue + “)”, 200);
setTimeout() 和 setInterval() 可被或一个函数或一个字符串作为首个参数。如果你传递一个字符串,引擎将创建一个新函数(使用函数构造器),这在一些浏览器中会非常慢。相反,传递函数本身作为首个参数,更快、更强大、更干净。
解决办法: 一定不要使用 strings for setTimeout() 或 setInterval()。
- function log1() { console.log(document.location); }
- function log2(arg) { console.log(arg); }
- var myValue = “test”;
- setTimeout(log1, 100); //Reference to a function
- setTimeout(function(){ //Get arg value using closures
- log2(arg);
- }, 200);
10.setInterval() 的用法 Usage of setInterval() for heavy functions
举例:
- function domOperations() {
- //Heavy DOM operations, takes about 300ms
- }
- setInterval(domOperations, 200);
setInterval() 将一函数列入计划被执行,仅是在没有另外一个执行在主执行队列中等待。JavaScript 引擎只增加下一个执行到队列如果没有另外一个执行已在队列。这可能导致跳过执行或者运行2个不同的执行,没有在它们之间等待200ms的情况下。
一定要搞清,setInterval() 没有考虑进多长时间domOperations() 来完成任务。
解决办法:避免 setInterval(),使用 setTimeout()
- function domOperations() {
- //Heavy DOM operations, takes about 300ms
- //After all the job is done, set another timeout for 200 ms
- setTimeout(domOperations, 200);
- }
- setTimeout(domOperations, 200);
11. ”this“的滥用 Misuse of ‘this’
这个常用错误,没有例子,因为非常难创建来演示。this的值在JavaScript中与其他语言有很大的不同。
函数中的this值被定义是在当函数被调用时,而非声明的时间,这一点非常重要。下面的案例中,函数内this有不同的含义。
* Regular function: myFunction(‘arg1’);
this points to the global object, wich is window for all browers.
* Method: someObject.myFunction(‘arg1’);
this points to object before the dot, someObject in this case.
* Constructor: var something = new myFunction(‘arg1’);
this points to an empty Object.
* Using call()/apply(): myFunction.call(someObject, ‘arg1’);
this points to the object passed as first argument.
12. eval()访问动态属性的用法 Usage of eval() to access dynamic properties
举例:
- var myObject = { p1: 1, p2: 2, p3: 3};
- var i = 2;
- var myResult = eval(‘myObject.p’+i);
主要问题在于使用eval() 开始一个新的执行语句,会非常的慢。
解决办法:使用方括号表示法(square bracket notation)代替 eval()。
- var myObject = { p1: 1, p2: 2, p3: 3};
- var i = 2;
- var myResult = myObject[“p”+i];
13. 未定义(undefined)作为变量的用法 Usage of undefined as a variable
举例:
- if ( myVar === undefined ) {
- //Do something
- }
在上面的例子中,未定义实际上是一变量。所有的JavaScript引擎会创建初始化的变量window.undefined 给未定义作为值。然而注意的是变量不仅是可读,任何其他的代码可以刚改它的值。很奇怪能找到window.undefined 有来自未定义的不同的值的场景,但是为什么冒险呢?
解决办法:检查未定义时,使用typeof。
- if ( typeof myVar === “undefined” ) {
- //Do something
- }
原文链接:http://my.oschina.net/qinqinbaby






