Tips for developing JavaScript

Source: Internet
Author: User
Tags setinterval

1, use = = = Replace = =
The = = and! = operators automatically convert data types when needed. but = = = and!== do not, they compare values and data types at the same time, which makes them faster than = = and! =.

1         if("1"=="1"){//Slow Speed2 3         }4         if("1"==="1") {//Fast Speed5 6         }7         if("1"!="1") {//Slow Speed8 9         }Ten         if("1"!=="1") {//Fast Speed One  A}

2,underfined, NULL, 0, False, NaN, the logical result of an empty string is false

3. Get members randomly from the array

var array=[,'a',5,8,9, 4,94,'fv']; var randomitem=array[math.floor (Math.random () * array.length)];

4. Get random numbers within a specified range

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

5. The function is automatically executed after creation, often referred to as a self-invocation of an anonymous function (self-invoked Anonymous function) or a direct call to a function expression (Immediately invoked functions expressions). The format is as follows:

         (function  () {        // automatic code Execution         }) ();         (function  (A, b        ) {// automatic code Execution        }) (4,9);

6. Append between arrays

1         var arrary1=[45,958,68,98,669,85,34]; 2         var arrary2=[7,54,8,54,57,9,77]; 3         Array.prototype.push.apply (Arrary1,arrary2);  // the value of the arrary1 is: [45,958,68,98,669,85,34,7,54,8,54,57,9,77]

7. Get the maximum and minimum values in the array

        var numbers=[46,4,68,89,87,84,49,16,89,9];         var maxnumber=math.max.apply (math,numbers);  // Maximum Value        var minnumber=math.min.apply (math,numbers);  // Minimum Value

8. Empty the array

var array=[15,9,68,76];array.length= 0;

9, do not directly from the array delete or remove elements, if the array element directly using delete, actually did not delete, but the element is placed in order to undefined. array element deletions should use splice. You can use delete when you delete an object's properties.

Arrayobject.splice (index,howmany,item1,....., ItemX)
Parameters Description
Index Necessary. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array.
Howmany Necessary. The number of items to delete. If set to 0, the item is not deleted.
Item1, ..., ItemX Optional. Adds a new item to the array.
Description

The splice () method deletes 0 or more elements starting at index and replaces the deleted elements with one or more values declared in the argument list.

If the element is removed from the Arrayobject, the array containing the deleted element is returned.

10. Keep the specified number of decimal digits

var num=2.9932858; num//num=2.9932

11, floating point calculation problem
0.1+0.2 equals 0.30000000000000004. JavaScript numbers are built on the IEEE 754 standard and are internally represented by 64-bit floating-point decimals, as described in how numbers in JavaScript are encoded.
You can solve this problem by using tofixed () and toprecision ().

The ToFixed () method rounds numbers to a number that specifies the number of decimal digits.

Numberobject.tofixed (num)
Parameters Description
Num Necessary. Specifies the number of decimal places, which are values between 0 and 20, including 0 and 20, and some implementations can support a larger range of values. If this argument is omitted, 0 will be used instead.
return value

Returns a string representation of numberobject, with no exponential notation, and a fixed num digit number after the decimal point. If necessary, the number is rounded, or it can be topped with 0 so that it reaches the specified length. If NUM is greater than le+21, the method only calls Numberobject.tostring () and returns the string represented by exponential notation.

Thrown

Throws an exception rangeerror when NUM is too small or too large. A value between 0 and 20 does not throw the exception. Some implementations support values in a larger range or a smaller range.

The TypeError exception is thrown when the object calling the method is not number.

The Toprecision () method converts an object's value beyond the specified number of digits to exponential notation.

Numberobject.toprecision (num)
Parameters Description
Num Necessary. Specifies the minimum number of digits that must be converted to exponential notation. The parameter is a value between 1 and 21 (and includes 1 and 21). An effective implementation allows you to selectively support larger or smaller Num. If this argument is omitted, the method toString () is called instead of converting the number to a decimal value.
return value

Returns a string representation of numberobject containing num valid digits. If NUM is large enough to include all the digits of the Numberobject integer part, the returned string will be in the fixed-point notation. Otherwise, the exponential counting method, that is, there is a digit before the decimal point, there are num-1 digits after the decimal point. If necessary, the number is rounded or topped with 0.

Thrown

Throws an exception rangeerror when NUM is too small or too large. A value between 1 and 21 does not throw the exception. Some implementations support values in a larger range or a smaller range.

The TypeError exception is thrown when the object calling the method is not number.

12. Use functions instead of strings when passed to SetInterval () and settimeout ()

1     // do not use: 2       SetInterval ("fun_1 ()", +); 3        SetInterval ("Fun_2 ()", +); 4        // use: 5        SetInterval (fun_1,200); 6        SetInterval (fun_2,200);

Tips for developing JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.