Variables-memory-scope (JAVASCRIPT)

Source: Internet
Author: User

Basic types and reference types

The ECMAScript variable may contain values for two different data types: The base data type value and the reference type value.
The basic data types are: Undefined,null,boolean,number and string Five.
The reference type is: Object,array,date,regexp,function.

Basic Variable Value replication
var=11;var= a;console.log(b);//11=22;console.log(a);//22console.log(b);//11 //b still has not been changed after the a was changed

The code above shows that the two variables (A and B) after value replication (b copy A's value) can participate in any operation without affecting each other.

Object variable Value replication
varObj1= New Object();varObj2=Obj1;obj1.name = ' MUSTANG ';Console.Log(Obj2.name);//mustangobj1. Age =  at;Obj2. Age =  -;Console.Log(obj1. Age);//24

The above code illustrates: After OBJ2 copy obj1, two objects point to the same heap memory (that is, share a heap memory).
Therefore any object (OBJ1 or OBJ2) that adds/removes properties or methods is changed in heap memory (if two objects have the same property name or method name, whichever is the last, which is overwritten by the same name).

Passing of parameters
functionadd(num){    +=20;    return num;}var=10;var=add(a);console.log(result);//30console.log(a);//10

The above code illustrates that the parameter is passed by value, and if NUM is passed by reference, the value of a will also be 30. Because the reference pass is passed through the heap memory.

An interesting example.
//#1function SetName(obj){    obj.name = ' MUSTANG ';}varHorse= New Object();SetName(horse);Console.Log(Horse.name);//mustang//---------------------------------//#2function SetName(obj){    obj.name = ' MUSTANG ';Obj= New Object();    obj.name = ' SHELBY ';}varHorse= New Object();SetName(horse);Console.Log(Horse.name);//mustang//still MUSTANG

The above code description: Combined with # # #, the first execution setName() parameter is passed by value, but at this point the argument is an object, and the object is passed by reference.
Therefore, when executed setName() , the Obj object (that is, the person object) adds the Name property.
As mentioned earlier, object Add/Remove properties or methods are manipulated in heap memory, so the external person also adds name properties.
So console.log(horse.name) the execution gets ’MUSTANG’ rather than ’SHELBY’ .
The reason why this obj = new Object(); obj.name = ‘SHELBY‘; is not effective is that obj is a local variable at this point, and obj is another heap memory at this point, so it does not affect the external results.

Article Description:

Reference: JavaScript Advanced Programming (3rd Edition), chapter Fourth

The level is limited, please correct me.

Variables-memory-scope (JAVASCRIPT)

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.