JS语法技巧经验篇(一)
JS语法技巧经验篇(二)
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 */
undefined并不是JavaScript中的保留字,尽管它有特殊的意义,并且是唯一的方法确定变量是否未定义。(杜绝该做法)因此:
var someVar; alert(someVar==undefined); //显示 true undefined="I'm not undefined!"; alert(someVar == undefined);//显示 false!
0.1 + 0.2 !== 0.3; // true var num =2.443242342; num = num.toFixed(4); // num will be equal to 2.4432
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);
对于简单的任务,最好使用基本操作方式来实现,而不是使用函数调用实现。
// 调用函数 var min = Math.min(a,b); A.push(v); // 原生操作 var min = a < b ? a b; A[A.length] = v;
isFinite(0/0) ; // false isFinite("foo"); // false isFinite("10"); // true isFinite(10); // true isFinite(undifined); // false isFinite(); // false isFinite(null); // true !!!
如果有超过2个以上的case,那么使用switch/case速度会快很多,而且代码看起来更加优雅。
可以将loop套在try代码块中,然后进行catch捕获。
[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
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 × 1, 5478, "foo", 8852, undefined × 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 × 1, "Doe", 2154, 119] */ The delete method should be used to delete an object property.
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
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技巧及最佳实践