Javascript variables and scopes

Source: Internet
Author: User

I. Copy the variable value
JavaScript has two types of values: basic type value and reference type value. The replication results for these two types of values are different.
Basic Type Value
1 var num1 = 5;
2 var num2 = num1;
The value of num2 will be assigned to 5, and even deleting num1 will not affect it.
Reference Type Value
1 var obj1 = new Object ();
2 obj1.age = 5;
3 var obj2 = obj1;
Although obj2.age is also 5, obj1 and obj2 actually point to the same object. If obj1 is deleted, obj2 does not exist.
 
Ii. parameter transfer
Parameters in JavaScript are passed by value. That is, during the transfer process, the values outside the function are copied to the parameter.
Therefore, the following results are obtained:
Basic Type Value
 
1 var num1 = 5;
2 function setValue (obj ){
3 return obj + = 10;
4}
5 var num2 = setValue (num1 );
6 alert (num1); // 5
7 alert (num2); // 15
 
Because this is a replication process, basic type replication does not affect the original value, so the parameter calculation in the function does not change the value of num1.
Reference Type Value
 
1 function setAge (obj ){
2 obj. age = 15;
3}
4 var john = new Object ();
5 setAge (john );
6 alert (john. age); // 15
 
Similarly, because of the replication of application type values, if the parameter is re-copied in the function, the original values outside the function will also be affected.
Iii. Scope
And java, c, and other languages. JavaScript has no block-level scope. The so-called block-level scope is the execution environment held up.
For example:
 
1 (function (){
2 var I = 5;
3 if (I = 5 ){
4 var color = "blue ";
5}
6 alert (color );
7 })();
 
In similar code, an error is reported in java because the color is destroyed after the if condition is executed. However, JavaScript can be executed normally.
No block-level scope does not mean no execution environment.
Run the following code:
 
1 (function (){
2 var I = 5;
3 if (I = 5 ){
4 var color = "blue ";
5}
6 })();
7 alert (color );
 
Because color is declared in the function, the execution environment of the function is destroyed after the function is completed, and the color does not exist, an error is reported.

 


Excerpted from zookeeper website

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.