第一次给变量赋值时,别忘记var关键字
给一个未声明的变量赋值,该变量会被自动创建为全局变量,在JS开发中,应该避免使用全局变量。
2.使用===替换==
并且永远不要使用=或!=。
- [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
[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 false3.使用分号来作为行终止字符
在行终止的地方使用分号是一个很好的习惯,即使开发人员忘记加分号,编译器也不会有任何提示,因为在大多数情况下,JavaScript解析器会自动加上。
4.创建构造函数
- function Person(firstName, lastName){
- this.firstName = firstName;
- this.lastName = lastName;
- }
- var Saad = new Person("Saad", "Mousliki");
function Person(firstName, lastName){ this.firstName = firstName; this.lastName = lastName; } var Saad = new Person("Saad", "Mousliki");5.应当小心使用typeof、instanceof和constructor
- var arr = ["a", "b", "c"];
- typeof arr; // return "object"
- arr instanceof Array // true
- arr.constructor(); //[]
var arr = ["a", "b", "c"]; typeof arr; // return "object" arr instanceof Array // true arr.constructor(); //[]6.创建一个Self-calling函数
这通常会被称为自我调用的匿名函数或立即调用函数表达式(LLFE)。当函数被创建的时候就会自动执行,好比下面这个:
- (function(){
- // some private code that will be executed automatically
- })();
- (function(a,b){
- var result = a+b;
- return result;
- })(10,20)
(function(){ // some private code that will be executed automatically })(); (function(a,b){ var result = a+b; return result; })(10,20)7.给数组创建一个随机项
- var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119];
- var randomItem = items[Math.floor(Math.random() * items.length)];
var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119]; var randomItem = items[Math.floor(Math.random() * items.length)];8.在特定范围里获得一个随机数
下面这段代码非常通用,当你需要生成一个假的数据用来测试时,比如在最低工资和最高之前获取一个随机值。
- var x = Math.floor(Math.random() * (max - min + 1)) + min;
var x = Math.floor(Math.random() * (max - min + 1)) + min;9.在数字0和最大数之间生成一组随机数
- var numbersArray = [] , max = 100;
- for( var i=1; numbersArray.push(i++) < max;); // numbers = [0,1,2,3 ... 100]
var numbersArray = [] , max = 100; for( var i=1; numbersArray.push(i++) < max;); // numbers = [0,1,2,3 ... 100]10.生成一组随机的字母数字字符
- function generateRandomAlphaNum(len) {
- var rdmstring = "";
- for( ; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
- return rdmString.substr(0, len);
- }
function generateRandomAlphaNum(len) { var rdmstring = ""; for( ; rdmString.length < len; rdmString += Math.random().toString(36).substr(2)); return rdmString.substr(0, len); }11.打乱数字数组
- var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
- numbers = numbers.sort(function(){ return Math.random() - 0.5});
- /* the array numbers will be equal for example to [120, 5, 228, -215, 400, 458, -85411, 122205] */
var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411]; numbers = numbers.sort(function(){ return Math.random() - 0.5}); /* the array numbers will be equal for example to [120, 5, 228, -215, 400, 458, -85411, 122205] */12.字符串tim函数
trim函数可以删除字符串两端的空白字符,可以用在Java、C#、PHP等多门语言里。
- String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g, "");};
String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g, "");};13.数组追加
- var array1 = [12 , "foo" , {name "Joe"} , -2458];
- var array2 = ["Doe" , 555 , 100];
- Array.prototype.push.apply(array1, array2);
- /* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */
var array1 = [12 , "foo" , {name "Joe"} , -2458]; var array2 = ["Doe" , 555 , 100]; Array.prototype.push.apply(array1, array2); /* array1 will be equal to [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */14.将参数对象转换为数组
- var argArray = Array.prototype.slice.call(arguments);
var argArray = Array.prototype.slice.call(arguments);15.验证一个给定参数是否为数字
- function isNumber(n){
- return !isNaN(parseFloat(n)) && isFinite(n);
- }
function isNumber(n){ return !isNaN(parseFloat(n)) && isFinite(n); }16.验证一个给定的参数为数组
- function isArray(obj){
- return Object.prototype.toString.call(obj) === '[object Array]' ;
- }
function isArray(obj){ return Object.prototype.toString.call(obj) === '[object Array]' ; }
注意,如果toString()方法被重写了,你将不会得到预期结果。
或者你可以这样写:
- Array.isArray(obj); // its a new Array method
Array.isArray(obj); // its a new Array method同样,如果你使用多个frames,你可以使用instancesof,如果内容太多,结果同样会出错。
- var myFrame = document.createElement('iframe');
- document.body.appendChild(myFrame);
- var myArray = window.frames[window.frames.length-1].Array;
- var arr = new myArray(a,b,10); // [a,b,10]
- // instanceof will not work correctly, myArray loses his constructor
- // constructor is not shared between frames
- arr instanceof Array; // false
var myFrame = document.createElement('iframe'); document.body.appendChild(myFrame); var myArray = window.frames[window.frames.length-1].Array; var arr = new myArray(a,b,10); // [a,b,10] // instanceof will not work correctly, myArray loses his constructor // constructor is not shared between frames arr instanceof Array; // false17.从数字数组中获得最大值和最小值
- var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
- var maxInNumbers = Math.max.apply(Math, numbers);
- var minInNumbers = Math.min.apply(Math, numbers);
var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411]; var maxInNumbers = Math.max.apply(Math, numbers); var minInNumbers = Math.min.apply(Math, numbers);18.清空数组
- var myArray = [12 , 222 , 1000 ];
- myArray.length = 0; // myArray will be equal to [].
var myArray = [12 , 222 , 1000 ]; myArray.length = 0; // myArray will be equal to [].19.不要用delete从数组中删除项目
开发者可以使用split来代替使用delete来删除数组项。与其删除数组中未定义项目,还不如使用delete来替代。
- 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] */
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] */也可以……
- 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] */
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] */
delete方法应该删除一个对象属性。
20.使用length属性缩短数组
如上文提到的清空数组,开发者还可以使用length属性缩短数组。
- var myArray = [12 , 222 , 1000 , 124 , 98 , 10 ];
- myArray.length = 4; // myArray will be equal to [12 , 222 , 1000 , 124].
var myArray = [12 , 222 , 1000 , 124 , 98 , 10 ]; myArray.length = 4; // myArray will be equal to [12 , 222 , 1000 , 124].如果你所定义的数组长度值过高,那么数组的长度将会改变,并且会填充一些未定义的值到数组里,数组的length属性不是只读的。
- myArray.length = 10; // the new array length is 10
- myArray[myArray.length - 1] ; // undefined