Home > Web Front-end > JS Tutorial > body text

Summary of common Javascript tips_javascript tips

WBOY
Release: 2016-05-16 15:53:03
Original
1063 people have browsed it

The examples in this article describe common Javascript tips. Share it with everyone for your reference. The specific analysis is as follows:

1. True and False Boolean expressions

The following Boolean expressions all return false:

null
undefined
'' empty string
0 number 0

But be careful with the following, they all return true:

'0' string 0
[] Empty array
{} empty object

The following piece of terrible code:

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

You can directly write it in the following form (as long as you want x to be other than 0 and an empty string, and false):

Copy code The code is as follows:
while (x) {

If you want to check if a string is null or empty:

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

But this would be better:

Copy code The code is as follows:
if (y) {

Note: There are still many things to pay attention to, 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

2. Conditional (ternary) operator (?:)

The ternary operator is used to replace the following code:

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

Copy after login

You can write:

Copy code The code is as follows:
return val ? foo() : bar();

Also useful when generating HTML code:
Copy code The code is as follows:
var html = '';

Three, && and ||

Binary Boolean operators are short-circuitable, and the last item will only be evaluated when necessary.

"||" is called the 'default' operator because it works like this:

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

Copy after login

You can use this to simplify the above code:

function foo(opt_win) {
 var win = opt_win || window;
 // ...
}
Copy after login

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

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

Copy after login

You can use it like this:

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

Copy after login

or:

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

Copy after login

But this is a bit too much:

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


4. Use join() to create a string

is usually used like this:

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

Copy after login

But this is very slow under IE. You can use the following method:

function listHtml(items) {
 var html = [];
 for (var i = 0; i < items.length; ++i) {
  html[i] = itemHtml(items[i]);
 }
 return '' + html.join(', ') + '';
}

Copy after login

You can also use an array as the string constructor, and then convert it into a string through myArray.join(''). However, since the assignment operation is faster than the push() of the array, try to use the assignment operation.

5. Traverse Node List

Node lists are implemented by adding a filter to the node iterator. This means that the time complexity of obtaining its attributes, such as length, is O(n), and traversing the entire list through length requires O(n^2 ).

var paragraphs = document.getElementsByTagName_r('p');
for (var i = 0; i < paragraphs.length; i++) {
 doSomething(paragraphs[i]);
}

Copy after login

This will be better:

var paragraphs = document.getElementsByTagName_r('p');
for (var i = 0, paragraph; paragraph = paragraphs[i]; i++) {
 doSomething(paragraph);
}

Copy after login

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

In the above example, you can also traverse the child nodes through firstChild and nextSibling.

var parentNode = document.getElementByIdx_x('foo');
for (var child = parentNode.firstChild; child; child = child.nextSibling) {
 doSomething(child);
}

Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!