【JS教程】JS语法技巧经验篇(一)

JS语法技巧经验篇(一)

JS语法技巧经验篇(二)

经验技巧篇:

1、JSON对象的序列化和反序列化
var person = {name :'Saad', age : 26, department : {ID : 15, name : "R&D"} };   
var stringFromPerson = JSON.stringify(person);   
/* stringFromPerson is equal to "{"name":"Saad","age":26,"department":{"ID":15,"name":"R&D"}}"   */   
var personFromString = JSON.parse(stringFromPerson);    
/* personFromString is equal to person object  */

2、undefined 可以被定义 defined

    undefined并不是JavaScript中的保留字,尽管它有特殊的意义,并且是唯一的方法确定变量是否未定义。(杜绝该做法)因此:

var someVar; 
alert(someVar==undefined); //显示 true
undefined="I'm not undefined!"; 
alert(someVar == undefined);//显示 false!

3、0.1 + 0.2 !== 0.3  && 舍入小数位数
0.1 + 0.2 !== 0.3;  // true

var num =2.443242342;  
num = num.toFixed(4);  // num will be equal to 2.4432

4、避免使用with()方法

    JavaScript 有个 with 关键字, with 语句的原本用意是为逐级的对象访问提供命名空间式的速写方式. 也就是在指定的代码区域, 直接通过节点名称调用对象.

    实例一:

var root = {
    branch: {
        node: 1
    }
};
with(root.branch) {
    node = 0;
    // 显示 0, 正确!
    alert(node);
}
// 显示 0, 正确!
alert(root.branch.node);

    实例二:

var root = {
    branch: {
        node: 1
    }
};
with(root.branch) {
    root.branch = {
        node: 0
};
// 显示 1, 错误!
alert(node);
}
// 显示 0, 正确!
alert(root.branch.node);

5、 最原始的操作要比函数调用快

    对于简单的任务,最好使用基本操作方式来实现,而不是使用函数调用实现。 

// 调用函数
var min = Math.min(a,b);   
A.push(v); 
// 原生操作
var min = a < b ? a b;   
A[A.length] = v;

6、利用 isFinite()判断是否为有限小数
isFinite(0/0) ; // false   
isFinite("foo"); // false   
isFinite("10"); // true   
isFinite(10);   // true   
isFinite(undifined);  // false   
isFinite();   // false   
isFinite(null);  // true  !!!

7、 使用switch/case语句代替较长的if/else语句:

如果有超过2个以上的case,那么使用switch/case速度会快很多,而且代码看起来更加优雅。

8、在一个loop里避免使用try-catch-finally:

可以将loop套在try代码块中,然后进行catch捕获。

9、不要忘记变量声明的关键字 var,否则,该变量将被声明为全局变量。 
10、使用 “===” 替代 “==”

[10] === 10    // is false
[10]  == 10    // is true
'10' == 10     // is true
'10' === 10    // is false
 []   == 0     // is true
 [] ===  0     // is false
 '' == false   // is true but true == "a" is false
 '' ===   false // is false

11、undefined, null, 0, false, NaN, '' (empty string) are all falsy. 
12、Use Semicolons for line termination 
13、Don’t use delete to remove an item from array

    Use splice instead of using delete to delete an item from an array. Using delete replaces the item with undefined instead of the removing it from the array.

    // Instead of…

var items = [12, 548 ,'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' ,2154 , 119 ]; 
items.length; // return 11 
delete items[3]; // return true 
items.length; // return 11 
/* items will be equal to [12, 548, "a", undefined &times; 1, 5478, "foo", 8852, undefined &times; 1, "Doe", 2154,       119]   */

    // Use…

var items = [12, 548 ,'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' ,2154 , 119 ]; 
items.length; // return 11 
items.splice(3,1) ; 
items.length; // return 10 
/* items will be equal to [12, 548, "a", 5478, "foo", 8852, undefined &times; 1, "Doe", 2154,       119]   */
The delete method should be used to delete an object property.

14、Truncate an array using length

    Like the previous example of emptying an array, we truncate it using the length property.

var myArray = [12 , 222 , 1000 , 124 , 98 , 10 ];  
myArray.length = 4; // myArray will be equal to [12 , 222 , 1000 , 124].

    As a bonus, if you set the array length to a higher value, the length will be changed and new items will be added with undefined as a value. The array length is not a read only property.

myArray.length = 10; // the new array length is 10 
myArray[myArray.length - 1] ; // undefined

15、 Null是个对象,NaN是个数值,空数组等同于false
  • alert(typeofnull); //弹出 'object'

  • alert(typeof NaN); //弹出 'Number' ,NaN本意是表示某个值不是数值,但是其本身却又是数值类型

  • alert(NaN===NaN); //为 false

  • alert(new Array()==false);//为 true

参考列表:

  • 45 Useful JavaScript Tips, Tricks and Best Practices

  • 超实用的JavaScript技巧及最佳实践



你可能感兴趣的:(【JS教程】JS语法技巧经验篇(一))