JS Variable Scope memory

Source: Internet
Author: User

Basic data type:
Undefined;null;boolean;number;string
These five basic types are accessed by value, because the actual values saved in the variable can be manipulated


Reference data type:
Object
Reference data types are objects that are stored in memory, and JavaScript does not allow direct access to the in-memory location, that is, the memory space of the object cannot be manipulated directly, and when manipulating the object, it is actually the object's reference, not the actual object. When copying a variable that holds an object, the object's reference is manipulated. However, when you add an attribute to an object, the actual object is manipulated)

Parameter passing:
All functions in JavaScript are passed by value, so when a function's formal parameter (in the concept of ECMAScript, which is an element in a arguments object) is a reference data type, it is also passed by value: it copies the address of the object to the parameter, remembering that it is not a reference

eg

function Addten (num) {Num+=10;return num;} var count=20;var result=addten (count), alert (count),//20 value does not change when count copies the value to the parameter num, it is not related to count alert (result);//30

  


Another example:
eg

function Setnamea (obj) {obj.name: "Monica";} var Person={};setnamea (person); alert (person.name);//"Monica" They only use the same address, although it is passed by value, but the effect is the same as when the reference is passed, so it is often wrongly considered a reference to pass

  

Take a look at this example:
eg

function Setnameb (obj) {obj.name= "Monica"; obj = new Object (); Obj.name= "Green";} var person={};setnameb (person); alert (person.name);//monica (closure)

  


Detection type: typeof and Instanceof

var a=undefined;    Alert (typeof a);//undefinedvar b=null;    Alert (typeof b);//objectvar c=true;    Alert (typeof c);//booleanvar d=1;    Alert (typeof D);//numbervar e= "Monica";  Alert (typeof e);//stringvar f=new Object (); Alert (typeof f);//object

  


Instanceof returns TRUE or False

var Person{};alert (person instanceof object),//instanceof is a object?    Truealert (person instanceof Array);  Instanceof is a Array?    Falsealert (person instanceof REGEXP);  Instanceof is a RegExp? False

  

JS Variable Scope memory

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.