开发者最容易犯的13个JavaScript错误(2)_广州|网站建设|网页设计|网站设计|
0 Comments

开发者最容易犯的13个JavaScript错误(2)

发布于:1970-01-01  |   作者:广州网站建设  |   已聚集:人围观

7. 区别整数数字和浮点数字 Differentiate float numbers from integer numbers

举例:


  1. var myNumber = 3.5;  
  2. var myResult = 3.5 + 1.0; //We use .0 to keep the result as float 

在JavaScript中,浮点与整数间没有区别。事实上,JavaScript中的每个数字都表示使用双精度64位格式IEEE 754。简单理解,所有数字都是浮点。

解决办法:不要使用小数(decimals),转换数字(numbers)到浮点(floats)。
广州网站建设,网站建设,广州网页设计,广州网站设计


  1. var myNumber = 3.5;  
  2. var myResult = 3.5 + 1; //Result is 4.5, as expected 

8. with()作为快捷方式的用法 Usage of with() as a shortcut

举例:


  1. team.attackers.myWarrior = { attack: 1, speed: 3, magic: 5};  
  2. with (team.attackers.myWarrior){  
  3.     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,如果你确实需要时。


  1. team.attackers.myWarrior = { attack: 1, speed: 3, magic: 5};  
  2. var sc = team.attackers.myWarrior;  
  3. console.log(“Your warrior power is ” + (sc.attack * sc.speed)); 

9.setTimeout/setInterval 字符串的用法 Usage of strings with setTimeout/setInterval

举例:


  1. function log1() { console.log(document.location); }  
  2. function log2(arg) { console.log(arg); }  
  3. var myValue = “test”;  
  4. setTimeout(“log1()”, 100);  
  5. setTimeout(“log2(” + myValue + “)”, 200); 

setTimeout() 和 setInterval() 可被或一个函数或一个字符串作为首个参数。如果你传递一个字符串,引擎将创建一个新函数(使用函数构造器),这在一些浏览器中会非常慢。相反,传递函数本身作为首个参数,更快、更强大、更干净。

解决办法: 一定不要使用 strings for setTimeout() 或 setInterval()。


  1. function log1() { console.log(document.location); }  
  2. function log2(arg) { console.log(arg); }  
  3. var myValue = “test”;  
  4. setTimeout(log1, 100); //Reference to a function  
  5. setTimeout(function(){ //Get arg value using closures  
  6.     log2(arg);  
  7. }, 200); 

10.setInterval() 的用法 Usage of setInterval() for heavy functions
广州网站建设,网站建设,广州网页设计,广州网站设计

举例:


  1. function domOperations() {  
  2.     //Heavy DOM operations, takes about 300ms  
  3. }  
  4. setInterval(domOperations, 200); 

setInterval() 将一函数列入计划被执行,仅是在没有另外一个执行在主执行队列中等待。JavaScript 引擎只增加下一个执行到队列如果没有另外一个执行已在队列。这可能导致跳过执行或者运行2个不同的执行,没有在它们之间等待200ms的情况下。

一定要搞清,setInterval() 没有考虑进多长时间domOperations() 来完成任务。

解决办法:避免 setInterval(),使用 setTimeout()


  1. function domOperations() {  
  2.     //Heavy DOM operations, takes about 300ms  
  3.     //After all the job is done, set another timeout for 200 ms  
  4.     setTimeout(domOperations, 200);  
  5. }  
  6. 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

举例:


  1. var myObject = { p1: 1, p2: 2, p3: 3};  
  2. var i = 2;  
  3. var myResult = eval(‘myObject.p’+i); 

主要问题在于使用eval() 开始一个新的执行语句,会非常的慢。

解决办法:使用方括号表示法(square bracket notation)代替 eval()。


  1. var myObject = { p1: 1, p2: 2, p3: 3};  
  2. var i = 2;  
  3. var myResult = myObject[“p”+i]; 

13. 未定义(undefined)作为变量的用法 Usage of undefined as a variable

举例:


  1. if ( myVar === undefined ) {  
  2.     //Do something  
  3. 标签:
飞机