Five small examples that help you understand JavaScript core closures and scopes _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces five small examples that can help you understand the core closures and scopes of JavaScript. This article is an article translated from abroad, for more information, see five small scripts to help you understand the closure and scope of the JavaScript core. Before running on the console, try to answer what will pop up in each case. Then you can create a test file to check your answer. Are you ready?

1,

The Code is as follows:


If (! ("A" in window )){
Var a = 1;
}
Alert ();


2,

The Code is as follows:


Var a = 1,
B = function a (x ){
X & a (-- x );
};
Alert ();


3,

The Code is as follows:


Function a (x ){
Return x * 2;
}
Var;
Alert ();


4,

The Code is as follows:


Function B (x, y, ){
Arguments [2] = 10;
Alert ();
}
B (1, 2, 3 );


5,

The Code is as follows:


Function (){
Alert (this );
}
A. call (null );


My predictions are: undefined, 1, unknown, 10, null

The answer is at the end of this Article. before reading the answer, do you dare to leave your guesses?

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

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.