What you may not know about JavaScript 1-5

Source: Internet
Author: User

1. The scope of the variable declared by the VaR keyword is the entire function.

var bar = 'global';function foo () {    console.log(bar); // ==> undefined    var bar = 'local';    console.log(bar); // ==> local}foo();

According to our ideas, the first line should be printed to global. however, in fact, because the scope of VaR is the whole function, rather than the content after it, our function definition is actually equivalent to the following definition.

function foo () {    var bar;    console.log(bar);    bar = 'local';    console.log(bar);}

We recommend that you put all the VaR statements at the top of the function to avoid unnecessary misunderstanding and increase the readability of the program.

PS:

function foo () {    for (var s=0, i=0; i<10; i++) {        for (var j=0; j<10; j++) {            s = i*j;        }    }    console.log(s); // ==> 81}foo();

In the above Code, S is generated in the for loop, but it does not hinder its scope. Its scope is still the whole function.

2. Scope chain

Waiting

3. | Operator

| The order of solution is from left to right. If the value on the left is true, the value of the entire formula must be true. Therefore, it will not be solved to the right.

For example

var i = 1;i || ++i;console.log(i); // ==> 1++i || i;console.log(i); // ==> 2

When you evaluate the I | ++ I expression, the system returns the result from left to right. Therefore, we can see that I, I is 1 and convert it to a Boolean value that is true. therefore, the value of the entire expression is true. Therefore, the ++ I.

| The expression also has a feature. The returned value is not necessarily true or false, but the value of the factor that forms the expression. You can see it in the code.

console.log(42 || [1,2]); // ==> 42

In fact, | the operator is equivalent to the following functions:

function _or_ (a, b) {    if (a) {        return a;    } else {        return b;    }}

I know you don't believe it, but you can experiment with it.

So some of our code can be written smoothly. For example, the default parameter can be written as follows to achieve our purpose:

function sayTo(who) {    if (!who) {        who = 'Xiaochi';    }    ...}

But there are more concise and easier-to-read methods:

function sayTo(who) {    who = who || 'Xiaochi';    ...}

PS:

& Operator is equivalent

function _and_ (a, b) {    if (a)        return b;    else        return a;}

4. The length of the array can be written.

Assign a value to the length of the array. If the value is smaller than the current value, the array is cut off. If the value is greater than the current value, the array is expanded.
I don't know how to use this knowledge point...

var a = [1];a.push(23);console.log(a); // ==> [1, 23]a.length = 5;console.log(a); // ==> [1, 23, undefined, undefined, undefined]

5. Convert parameters into Arrays

Sometimes you want to convert the function into an array object, so that you can use the various convenient methods of the array object. The method is as follows:

var argsArray = [].slice.apply(arguments);

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.