JavaScript Common Tips Summary _javascript tips

Source: Internet
Author: User

The examples in this article describe the common tips for JavaScript. Share to everyone for your reference. The specific analysis is as follows:

Boolean expressions of one, True, and False

The following Boolean expression returns false:

Null
Undefined
' Empty string
0 Number 0

But be careful of the following, all returns true:

' 0 ' string 0
[] Empty array
{} Empty Object

The following is a bad code:

Copy Code code as follows:
while (x!= null) {

You can write directly in the form below (as long as you want X to be not 0 and empty strings, and false):

Copy Code code as follows:
while (x) {

If you want to check whether the string is null or empty:

Copy Code code as follows:
if (y!= null && y!= ') {

But that would be better:

Copy Code code as follows:
if (y) {

Note: There are a lot of places to look for, such as:

Boolean (' 0 ') = = True
' 0 '!= true
0!= NULL
0 = []
0 = False
Boolean (NULL) ==false
Null!= True
Null!= False
Boolean (undefined) ==false
Undefined!= True
Undefined!= false
Boolean ([]) = = True
[]!= True
[] = = False
Boolean ({}) = = True
{}!= True
{}!= False

The condition (ternary) operator (?:)

The ternary operator replaces the following code:

if (val!= 0) {return
 foo ();
} else {return
 bar ();
}

You can write:

Copy Code code as follows:
Return Val? Foo (): Bar ();

It is also useful when generating HTML code:
Copy Code code as follows:
var html = ';

Third, && and | |

Binary Boolean operators are short-circuit, and the last item is computed only if necessary.

"| |" is called the ' default ' operator because it can:

function foo (opt_win) {
 var win;
 if (Opt_win) {
  win = Opt_win;
 } else {
  win = window;
 }
 // ...
}

You can use it to simplify the code above:

function foo (opt_win) {
 var win = Opt_win | | window;
 // ...
}

"&&" can also be short code. For example:

if (node) {
 if (node.kids) {
  if (Node.kids[index]) {
   foo (node.kids[index]);
  }
 }


You can use it like this:

if (node && node.kids && Node.kids[index]) {
 foo (node.kids[index]);
}

Or:

var kid = node && node.kids && Node.kids[index];
if (kid) {
 foo (kid);
}

But that's a little overdone:

Copy Code code as follows:
Node && node.kids && Node.kids[index] && foo (node.kids[index]);


use Join () to create a string

This is usually used in this way:

function listhtml (items) {
 var html = ';
 for (var i = 0; i < items.length; ++i) {
 if (i > 0) {html = ', ';
 }
 HTML + itemhtml (Items[i]);
 html + = ';
 return html;
}

But this is very slow under IE, you can use the following methods:

function listhtml (items) {
 var html = [];
 for (var i = 0; i < items.length ++i) {
  Html[i] = itemhtml (items[i));
 Return ' + html.join (', ') + ';
}

You can also use an array as a string constructor, and then convert it to a string by Myarray.join ('). However, as assignment operations are faster than the array's push (), the assignment operation is used as much as possible.

V. Traversal of Node List

Node lists is implemented by adding a filter to the nodes iterator. This means getting his properties, such as the time complexity of length O (n), and traversing the entire list by length requires O (n^2).

var paragraphs = document.getelementsbytagname_r (' P ');
for (var i = 0; i < paragraphs.length i++) {
 dosomething (paragraphs[i]);


It would be better to do this:

var paragraphs = document.getelementsbytagname_r (' P ');
for (var i = 0, paragraph paragraph = paragraphs[i]; i++) {
 dosomething (paragraph);
}

This method applies to all collections and arrays (as long as the array does not contain falsy values).

In the above example, you can also traverse children's nodes through FirstChild and nextSibling.

var parentnode = document.getelementbyidx_x (' foo ');
for (var child = Parentnode.firstchild, child, child = child.nextsibling) {
 dosomething (child);
}

I hope this article will help you with your JavaScript programming.

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.