JavaScript tips for using

Source: Internet
Author: User



1 underfined, NULL, 0, False, NaN, empty string logical result is False


2 getting members randomly from an array
var items = [548, ' a ', 2, 5478, ' foo ', 8852,, ' Doe ', 2145, 119];
var Randomitem = Items[math.floor (Math.random () * items.length)];


3 Gets the random number in the specified range
var x = Math.floor (Math.random () * (Max-min + 1)) + min;


4 Gets 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);


5 emptying the array
var myArray = [12, 222, 1000];
myarray.length = 0; MyArray'll is equal to [].


6 Do not delete or remove elements directly from the array
Avoid:
var items = [548, ' a ', 2, 5478, ' foo ', 8852,, ' Doe ', 2154, 119];
Items.length; Return 11
Delete Items[3]; return True
Items.length; Return 11
/* Items results for [548, "a", undefinedx1, 5478, "foo", 8852, Undefinedx1, "Doe", 2154, 119] */


Instead, you should:
var items = [548, ' a ', 2, 5478, ' foo ', 8852,, ' Doe ', 2154, 119];
Items.length; Return 11
Items.splice (3,1);
Items.length; Return 10
/* Items results for [548, "a", 5478, "foo", 8852, Undefinedx1, "Doe", 2154, 119]
You can use delete when you delete an object's properties.
*/


7 using logic with or in conditions
Logic or can also be used to set default values, such as default values for function parameters.


function DoSomething (arg1) {
Arg1 = Arg1 | | 10; Arg1 would has ten as a default value if it ' s not already set
}


8 Checking the properties of an object through the for-in loop
The following usage can prevent the iteration from entering the object's prototype properties.


for (var name in object) {
if (Object.hasownproperty (name)) {
Do something with Name
}
}


9 serialization and deserialization with JSON
var person = {name: ' Saad ', age:26, department: {id:15, Name: ' R '}};
var Stringfromperson = json.stringify (person);
/* Stringfromperson result for ' {' name ': ' Saad ', ' age ': ', ' Department ': {' ID ': ' "Name ':" R/r}} "*/
var personfromstring = Json.parse (Stringfromperson);
/* The value of personfromstring is the same as the person object */


10 do not use for-in on arrays
Avoid:
var sum = 0;
for (var i in arraynumbers) {
Sum + = Arraynumbers[i];
}


But:
var sum = 0;
for (var i = 0, len = arraynumbers.length; i < Len; i++) {
Sum + = Arraynumbers[i];
}


Another benefit is that I and Len two variables are in the first declaration of the For Loop, and they are initialized only once, which is faster than this:
for (var i = 0; i < arraynumbers.length; i++)


11 primitive operators are faster than function calls


For example, generally don't do this:
var min = Math.min (A, b);
A.push (v);


This can be replaced by:
var min = a < b? A:B;
A[a.length] = v;






Reference:
Http://www.w3cschool.cc/w3cnote/js-45-tips.html

JavaScript tips for using

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.