Learn JavaScript Basics (2): Scope and scope chain

Source: Internet
Author: User

Scope and scope chain

JS syntax usage is very flexible, and a little attention on the pit. This episode analyzes the following scopes and scope chains. We start with a few questions and you can try to guess the answer in your heart.

Question One,

if (true) {    var str = "John Doe";} alert (str); // what is the popup value? 

Question Two,

function Add (NUM1, num2) {    var sum = num1 + num2;} Add (+); alert (sum)//popup value Yes? 

Question three,

var str1 = "Zhang San"; var str2 = "John Doe"; function fun1 () {    var str2 = "Harry";     var str3 = "Zheng";     + str2 +//Popup value Yes?
} fun1 ();

Question Four,

var num1 = ten; function fun1 () {    alert (NUM1);} function fun2 (FN) {    var num1 =n;     What is the popup value?
There is no block-level scope in JS

Answer one:

if (true) {var str = "John Doe";} alert (str);

We see that the pop-up value is "John Doe". This shows that there is no block-level scope in JS (this is different from the other languages we used to contact). Not only if this is the case, for, while ... Wait, that's all.

Such as:

For (var i = 0; i < i++) {}alert (i);

Only function scope and global scope in JS

We can only access the outer scope from the outer inner scope, and the outer scope cannot access the inner scope.

Answer two:

function Add (NUM1, num2) {var sum = num1 + num2;} Add (+); alert (sum)

(not reflected?) Because the report is abnormal. Do not believe F12 see) So, we are not access to sum.  Because Sun is part of the scope within the Add function. We can only access variable values from within the Add function to the global scope.

The scope chain in JS

Answer three:

var str1 = "Zhang San"; var str2 = "John Doe"; function fun1 () {var str2 = "Harry"; var str3 = "Zheng"; Alert (str1 + str2 + str3);} Fun1 ();

STR1 takes global scope, STR3 to fun1 function scope. Some of the doubts here are str2. The scope chain is introduced here.

In general, we say that we first take the variables from our own scopes, and then take them out of the parent scope. That's true, but sometimes it can be confusing. As answer four:

var num1 = 10;function fun1 () {alert (NUM1);} function Fun2 (FN) {var num1 = 12; fn ();} Fun2 (FUN1);

"Take variables from your own scope, and then go to the parent scope", where the parent scope of FUN1 is the global scope, not the scope in fun2 when the Fu () is called. So we can change this sentence to "take the variable from your own scope first, and then go to the parent scope of the self-defined place."

Analysis here for the moment. Follow up if there is a new understanding to add in.

This is a learning record, not a tutorial. You can point out mistakes, but don't be mean.

Original link: http://haojima.net/zhaopei/513.html

This article has been synchronized to the catalog index: learn JavaScript step

Welcome to Shanghai "Program Ape/Yuan", "Siege Lion" into the group: "Shanghai Ape" 229082941 into the group notice

Learn JavaScript Basics (2): Scope and scope chain

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.