Further explore JavaScript scope _ basic knowledge

Source: Internet
Author: User
Tags define function

The first rule of the Golden rule:

JS does not have block-level scopes (you can do it yourself closures or other methods), only function-level scope, functions outside the variable function can be found inside the function of the variables outside the find.

A try:

What is this for??

var a = ten;
function aaa () {//step-4
	alert (a);//step-5-> execute alert, at this time can only find the outside of the a=10 frame ten
}
function bbb () {//step-2
	var a =;
	AAA ();//step-3
}
//define function is useless, call is real so here is Step-1
BBB ();//step-1

In fact, we all understand the principle, it should be easy wrong, million change in their comprehensive.

Second try:

What is this for? Because B is not defined, so a is undefined,b is 10.

Article II of the Golden rule:

The search for variables is the nearest principle, to find the variables defined by Var, to find the outer layer when the nearest one is not found.

Look

What is this for? There are two reasons for this, one is pre-resolution, and the other is the nearest search.

var a=10;
function aaa () {
	alert (a);//undefined, when looking for a, will now look in the function, because of the role of the pre-resolution, at this time a is undefined, so never go to find out of the 10
	var a =;

	/* Pre
	-resolved var a
	alert (a);
	var a = 20;*/

}
aaa ();

Attention

This bar, the verification of the second, although the nearest principle, but is the nearest variable to find Var declaration, this is because there is no VAR declaration variable is global, here only modified a value. So the above is because in the function did not find a Var, so go outside to find, find it, so a on alert out of 10, but yes is a=20, A is indeed 20, but the time has not been implemented to that ~ ~

Oh, look.

The following example validates the function scope of JS:

This is because at the time of alert (a), the A in the BBB function is indeed 20, but it is local to the phrase alert (a), and alert (a) cannot find a in the BBB function, so it can't find a in the AAA function, so go outside and find it, I found the 10.

Article III of the Golden rule:

The precedence is equivalent when the parameter is the same as the local variable.

Cases:

Also: When passing a parameter, the base type passes the value, and the reference type passes. (But after the assignment, it's not the case.)

var a = 5;
var b = A;
b +=3;
alert (a);//5

var a = [1,2,3];
var b=a;
B.push (4);
alert (a);//[1,2,3,4];

The code above is fine, but the following is different.

Since B has been assigned a value, it does not point to a.

In addition, the parameters are similar to the scope of the variables:

Compare the top and bottom two:

The above argument is the basic type, only the values are passed in, the following reference type: (also contains the case of the re-assignment)

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.