5 Small example _javascript tips that will help you understand JavaScript core closures and scopes

Source: Internet
Author: User

Here are 5 small scripts that help you really understand the JavaScript core-closures and scopes. Before the console runs, try to answer what pops up in each case, and then you can create a test file to check your answers. Are you ready?

1,

Copy Code code as follows:

if (! () A "in Window") {
var a = 1;
}
alert (a);

2,
Copy Code code as follows:

var a = 1,
b = function A (x) {
X && A (--x);
};
alert (a);

3,
Copy Code code as follows:

function A (x) {
return x * 2;
}
var A;
alert (a);

4,
Copy Code code as follows:

function b (x, Y, a) {
ARGUMENTS[2] = 10;
alert (a);
}
B (1, 2, 3);

5,
Copy Code code as follows:

function A () {
alert (this);
}
A.call (NULL);

My prediction answers were: Undefined, 1, unknown, 10, null.

The answer is at the end of this article, before you look at the answer, do you dare to leave your guess?

Correct answer: 1, undefined 2, 1 3, function A (x) {return x * 2} 4, 10 5, [object window]

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.