Home  >  Article  >  Web Front-end  >  45 JavaScript Development Tips

45 JavaScript Development Tips

黄舟
黄舟Original
2017-02-24 13:17:241380browse

JavaScript is a programming language that is the most popular in the world and can be used for web development, mobile application development (PhoneGap, Appcelerator), server-side development (Node.js and Wakanda), etc. JavaScript is also the first language for many novices to enter the world of programming. It can be used to display a simple prompt box in the browser, or to control the robot through nodebot or nodruino. Developers who can write JavaScript code with clear structure and high performance are now the most sought after people in the recruitment market.

In this article, I will share some JavaScript tips, tricks, and best practices, except for a few, whether it is the browser's JavaScript engine, or the server-side JavaScript interpreter, All applicable.

The sample code in this article passed the test on the latest version of Google Chrome 30 (V8 3.20.17.15).

1. Be sure to use the var keyword when assigning a value to a variable for the first time.

If a variable is assigned directly without a declaration, it will be used as a new global variable by default. Try to avoid using global variables.

2. Use === instead of ==

== and the != operator will automatically convert the data type when necessary. But === and !== don't, they compare values ​​and data types at the same time, which also makes them faster than == and !=.

[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

3. The logical results of underfined, null, 0, false, NaN, and empty strings are all false

4. Use a semicolon at the end of the line

In practice, the most common It's best to use a semicolon. It doesn't matter if you forget to write it. In most cases, the JavaScript interpreter will add it automatically. For more information on why semicolons are used, please refer to the article The Truth About Semicolons in JavaScript.

5. Use object constructor

function Person(firstName, lastName){
    this.firstName =  firstName;
    this.lastName = lastName;
}
var Saad = new Person("Saad", "Mousliki");

6. Use typeof, instanceof and constructor carefully

  • ##typeof: JavaScript unary operator, used for The original type of the variable is returned in the form of a string. Note that typeof null will also return object. Most object types (array Array, time Date, etc.) will also return object

  • contructor: Internal prototype properties can be overridden through code

  • instanceof: JavaScript operator, which will search in the constructor in the prototype chain and return true if found, otherwise false

  • var arr = ["a", "b", "c"];
    typeof arr;   // 返回 "object" 
    arr instanceof Array // true
    arr.constructor();  //[]
7. Use self-invoking functions

The function is automatically executed directly after creation. It is usually called a self-invoked anonymous function (Self-Invoked Anonymous Function) or a direct calling function expression. (Immediately Invoked Function Expression). The format is as follows:

(function(){
    // 置于此处的代码将自动执行
})();  
(function(a,b){
    var result = a+b;
    return result;
})(10,20)

8. Get random members from the array

var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119];
var  randomItem = items[Math.floor(Math.random() * items.length)];

9. Get random numbers within the specified range

This function is used when generating fake data for testing There are special numbers, such as wages within a specified range.

var x = Math.floor(Math.random() * (max - min + 1)) + min;

10. Generate a numeric array from 0 to the specified value

var numbersArray = [] , max = 100;
for( var i=1; numbersArray.push(i++) < max;);  // numbers = [1,2,3 ... 100]

11. Generate a random alphanumeric string

function generateRandomAlphaNum(len) {
    var rdmString = "";
    for( ; rdmString.length < len; rdmString  += Math.random().toString(36).substr(2));
    return  rdmString.substr(0, len);
}

12. Disorganize the order of the numeric array

var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
numbers = numbers.sort(function(){ return Math.random() - 0.5});
/* numbers 数组将类似于 [120, 5, 228, -215, 400, 458, -85411, 122205]  */

The built-in array sorting function of JavaScript is used here. A better way is to use special code to implement it (such as Fisher-Yates algorithm). You can see this discussion on StackOverFlow.

13. String spaces removal

Java, C#, PHP and other languages ​​have implemented special string space removal functions, but they are not available in JavaScript. You can use the following code to do this. String object function a trim function:

String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g, "");};

The new JavaScript engine already has a native implementation of trim().

14. Append between arrays

var array1 = [12 , "foo" , {name "Joe"} , -2458];
var array2 = ["Doe" , 555 , 100];
Array.prototype.push.apply(array1, array2);
/* array1 值为  [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */

15. Convert objects to arrays


var argArray = Array.prototype.slice.call(arguments);


16. Verify whether it is a number

function isNumber(n){
    return !isNaN(parseFloat(n)) && isFinite(n);
}

17. Verify whether it is an array

function isArray(obj){
    return Object.prototype.toString.call(obj) === &#39;[object Array]&#39; ;
}

But if the toString() method is overridden, it will not work. You can also use the following method:

Array.isArray(obj); // its a new Array method

If you do not use frame in the browser, you can also use instanceof, but if the context is too complex, errors may occur.

var myFrame = document.createElement(&#39;iframe&#39;);
document.body.appendChild(myFrame);
var myArray = window.frames[window.frames.length-1].Array;
var arr = new myArray(a,b,10); // [a,b,10]  
// myArray 的构造器已经丢失,instanceof 的结果将不正常
// 构造器是不能跨 frame 共享的
arr instanceof Array; // false

18. Get the maximum and minimum values ​​in the array

var  numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411]; 
var maxInNumbers = Math.max.apply(Math, numbers); 
var minInNumbers = Math.min.apply(Math, numbers);

19. Clear the array

var myArray = [12 , 222 , 1000 ];  
myArray.length = 0; // myArray will be equal to [].

20. Do not delete or remove elements directly from the array

If you use delete directly on an array element, it is not deleted, but the element is set to undefined. Array element deletion should use splice.

Don’t:

var items = [12, 548 ,&#39;a&#39; , 2 , 5478 , &#39;foo&#39; , 8852, , &#39;Doe&#39; ,2154 , 119 ]; 
items.length; // return 11 
delete items[3]; // return true 
items.length; // return 11 
/* items 结果为 [12, 548, "a", undefined × 1, 5478, "foo", 8852, undefined ×
 1, "Doe", 2154, 119] */

Instead:

var items = [12, 548 ,&#39;a&#39; , 2 , 5478 , &#39;foo&#39; , 8852, , &#39;Doe&#39; ,2154 , 119 ]; 
items.length; // return 11 
items.splice(3,1) ; 
items.length; // return 10 
/* items 结果为 [12, 548, "a", 5478, "foo", 8852, undefined × 1, "Doe", 2154, 119]

You can use delete when deleting the properties of an object.

21. Use the length attribute to truncate the array

In the previous example, the length attribute is used to clear the array. It can also be used to truncate the array:

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

At the same time, if As the length attribute becomes larger, the length value of the array will increase, and undefined will be used to fill in new elements. length is a writable property.

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

22. Use logical AND or in conditions

var foo = 10;  
foo == 10 && doSomething(); // is the same thing as if (foo == 10) doSomething(); 
foo == 5 || doSomething(); // is the same thing as if (foo != 5) doSomething();

Logical OR can also be used to set default values, such as the default values ​​of function parameters.

function doSomething(arg1){ 
    arg1 = arg1 || 10; // arg1 will have 10 as a default value if it’s not already set
}

23. Make the map() function method loop over the data

var squares = [1,2,3,4].map(function (val) {  
    return val * val;  
}); 
// squares will be equal to [1, 4, 9, 16]

24. Keep the specified number of decimal places

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

Note that toFixec() returns a string. Not a number.

25. Problems with floating point calculation

0.1 + 0.2 === 0.3 // is false 
9007199254740992 + 1 // is equal to 9007199254740992
9007199254740992 + 2 // is equal to 9007199254740994

Why? Because 0.1+0.2 is equal to 0.30000000000000004. JavaScript numbers are constructed in accordance with the IEEE 754 standard and are internally represented as 64-bit floating point decimals. For details, please refer to how numbers in JavaScript are encoded.

You can use toFixed() and toPrecision() to solve this problem.

26、通过for-in循环检查对象的属性

下面这样的用法,可以防止迭代的时候进入到对象的原型属性中。

for (var name in object) {  
    if (object.hasOwnProperty(name)) { 
        // do something with name
    }  
}

27、逗号操作符

var a = 0; 
var b = ( a++, 99 ); 
console.log(a);  // a will be equal to 1 
console.log(b);  // b is equal to 99

28、临时存储用于计算和查询的变量

在jQuery选择器中,可以临时存储整个DOM元素。

var navright = document.querySelector(&#39;#right&#39;); 
var navleft = document.querySelector(&#39;#left&#39;); 
var navup = document.querySelector(&#39;#up&#39;); 
var navdown = document.querySelector(&#39;#down&#39;);

29、提前检查传入isFinite()的参数

isFinite(0/0) ; // false
isFinite("foo"); // false
isFinite("10"); // true
isFinite(10);   // true
isFinite(undefined);  // false
isFinite();   // false
isFinite(null);  // true,这点当特别注意

30、避免在数组中使用负数做索引

var numbersArray = [1,2,3,4,5];
 var from = numbersArray.indexOf("foo") ; // from is equal to -1
 numbersArray.splice(from,2); // will return [5]

注意传给splice的索引参数不要是负数,当是负数时,会从数组结尾处删除元素。

31、用JSON来序列化与反序列化

var person = {name :&#39;Saad&#39;, age : 26, department : {ID : 15, name : "R&D"} };
var stringFromPerson = JSON.stringify(person);
/* stringFromPerson 结果为 "{"name":"Saad","age":26,"department":{"ID":15,"name":"R&D"}}"   */
var personFromString = JSON.parse(stringFromPerson);
/* personFromString 的值与 person 对象相同  */

32、不要使用eval()或者函数构造器

eval()和函数构造器(Functionconsturctor)的开销较大,每次调用,JavaScript引擎都要将源代码转换为可执行的代码。

var func1 = new Function(functionCode);
var func2 = eval(functionCode);

32、不要使用eval()或者函数构造器

eval()和函数构造器(Functionconsturctor)的开销较大,每次调用,JavaScript引擎都要将源代码转换为可执行的代码。

var func1 = new Function(functionCode);
var func2 = eval(functionCode);

33、避免使用with()

使用with()可以把变量加入到全局作用域中,因此,如果有其它的同名变量,一来容易混淆,二来值也会被覆盖。

34、不要对数组使用for-in

避免:

var sum = 0;  
for (var i in arrayNumbers) {  
    sum += arrayNumbers[i];  
}

而是:


var sum = 0;  
for (var i = 0, len = arrayNumbers.length; i < len; i++) {  
    sum += arrayNumbers[i];  
}


另外一个好处是,i和len两个变量是在for循环的第一个声明中,二者只会初始化一次,这要比下面这种写法快:

for (var i = 0; i < arrayNumbers.length; i++)

35、传给setInterval()和setTimeout()时使用函数而不是字符串

如果传给setTimeout()和setInterval()一个字符串,他们将会用类似于eval方式进行转换,这肯定会要慢些,因此不要使用:

setInterval(&#39;doSomethingPeriodically()&#39;, 1000);  
setTimeout(&#39;doSomethingAfterFiveSeconds()&#39;, 5000);

而是用:

setInterval(doSomethingPeriodically, 1000);  
setTimeout(doSomethingAfterFiveSeconds, 5000);

36、使用switch/case代替一大叠的if/else

当判断有超过两个分支的时候使用switch/case要更快一些,而且也更优雅,更利于代码的组织,当然,如果有超过10个分支,就不要使用switch/case了。

37、在switch/case中使用数字区间

其实,switch/case中的case条件,还可以这样写:

function getCategory(age) {  
    var category = "";  
    switch (true) {  
        case isNaN(age):  
            category = "not an age";  
            break;  
        case (age >= 50):  
            category = "Old";  
            break;  
        case (age <= 20):  
            category = "Baby";  
            break;  
        default:  
            category = "Young";  
            break;  
    };  
    return category;  
}  
getCategory(5);  // 将返回 "Baby"

38、使用对象作为对象的原型

下面这样,便可以给定对象作为参数,来创建以此为原型的新对象:

function clone(object) {  
    function OneShotConstructor(){}; 
    OneShotConstructor.prototype = object;  
    return new OneShotConstructor(); 
} 
clone(Array).prototype ;  // []

39、HTML字段转换函数

function escapeHTML(text) {  
    var replacements= {"<": "<", ">": ">","&": "&", "\"": """};                      
    return text.replace(/[<>&"]/g, function(character) {  
        return replacements[character];  
    }); 
}

40、不要在循环内部使用try-catch-finally

try-catch-finally中catch部分在执行时会将异常赋给一个变量,这个变量会被构建成一个运行时作用域内的新的变量。

切忌:

var object = [&#39;foo&#39;, &#39;bar&#39;], i;  
for (i = 0, len = object.length; i <len; i++) {  
    try {  
        // do something that throws an exception 
    }  
    catch (e) {   
        // handle exception  
    } 
}

而应该:

var object = [&#39;foo&#39;, &#39;bar&#39;], i;  
try { 
    for (i = 0, len = object.length; i <len; i++) {  
        // do something that throws an exception 
    } 
} 
catch (e) {   
    // handle exception  
}

41、使用XMLHttpRequests时注意设置超时

XMLHttpRequests在执行时,当长时间没有响应(如出现网络问题等)时,应该中止掉连接,可以通过setTimeout()来完成这个工作:

var xhr = new XMLHttpRequest (); 
xhr.onreadystatechange = function () {  
    if (this.readyState == 4) {  
        clearTimeout(timeout);  
        // do something with response data 
    }  
}  
var timeout = setTimeout( function () {  
    xhr.abort(); // call error callback  
}, 60*1000 /* timeout after a minute */ ); 
xhr.open(&#39;GET&#39;, url, true);  
xhr.send();

同时需要注意的是,不要同时发起多个XMLHttpRequests请求。

42、处理WebSocket的超时

通常情况下,WebSocket连接创建后,如果30秒内没有任何活动,服务器端会对连接进行超时处理,防火墙也可以对单位周期没有活动的连接进行超时处理。

为了防止这种情况的发生,可以每隔一定时间,往服务器发送一条空的消息。可以通过下面这两个函数来实现这个需求,一个用于使连接保持活动状态,另一个专门用于结束这个状态。

var timerID = 0; 
function keepAlive() { 
    var timeout = 15000;  
    if (webSocket.readyState == webSocket.OPEN) {  
        webSocket.send(&#39;&#39;);  
    }  
    timerId = setTimeout(keepAlive, timeout);  
}  
function cancelKeepAlive() {  
    if (timerId) {  
        cancelTimeout(timerId);  
    }  
}

keepAlive()函数可以放在WebSocket连接的onOpen()方法的最后面,cancelKeepAlive()放在onClose()方法的最末尾。

43、时间注意原始操作符比函数调用快,使用VanillaJS

比如,一般不要这样:

var min = Math.min(a,b); 
A.push(v);

可以这样来代替:

var min = a < b ? a : b; 
A[A.length] = v;

44、开发时注意代码结构,上线前检查并压缩JavaScript代码

别忘了在写代码时使用一个代码美化工具。使用JSLint(一个语法检查工具)并且在上线前压缩代码(比如使用JSMin)。注:现在代码压缩一般推荐 UglifyJS (http://www.php.cn/)

45、JavaScript博大精深,这里有些不错的学习资源

  • Code Academy资源:http://www.php.cn/

  • Marjin Haverbekex编写的Eloquent JavaScript:http://www.php.cn/

  • John Resig编写的Advanced JavaScript:http://www.php.cn/

 以上就是JavaScript 开发的45个技巧的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn