Another discussion on the scope of JS

Source: Internet
Author: User

Another discussion on the scope of JS

Topics encountered in the interview: Topic one: var word = "Hello world";  (function () {alert (word);  var word = "Hello test"; })();

Effect http://keleyi.com/keleyi/phtml/js/1.htm

Topic Two: var word = "Hello world";  (function () {alert (world);  function World () {console.log ("Hello Test"); }  })()

Effect http://keleyi.com/keleyi/phtml/js/1b.htm

What happens with these two pieces of code? Console a bit, you know.

The precedence of local variables is greater than global variables.

JS compilation mechanism JS is divided into the compilation period and run time, the process of compiling for VAR declared variables and *function* allocation of memory, run-time sequence execution.

var declares the variable, only the variable declaration is advanced. The initialization of the variable is still in its original position.

With function declaration statements, function names and function bodies are advanced. (In a function declaration statement, the function name is a variable name, pointing to the body of the function)

This article is reproduced from Collayi http://keleyi.com/a/bjad/cydygkca.htm

Another discussion on the scope of JS

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.