Learn JavaScript from the beginning (eight)--variables

Source: Internet
Author: User

Original: Learn JavaScript from the beginning (eight)--variables

First, the variable classification:

Base type value: null, Undefined, number, string, Boolean;

Reference type value: Keep an in-memory object, such as: Object/array/function/date/regexp/error/map/set ...

Second, the attribute

The two are defined in a similar way: Create a variable and assign it a value.

2.1 Base Type value cannot add delete attribute

2.2 Properties of a reference type can be dynamically added to delete properties

For example:

1 <script type= "Text/javascript" >2         varnew  Object (); 3         Person.name = "Nicholas"; 4         alert (person.name);    // "Nicholas" 5     </script>

The example above creates an object and saves it in the variable person, then we add a Name property to the object and assign a string to the property.

Let's look at an example:

1  <script type= "Text/javascript" >2         var name = "Nicholas"; 3         Name.age =; 4         alert (name.age);    // undefined 5     </script>

The value of a visible base type cannot be added dynamically, although it does not error.

Third, copy the value of the variable

3.1 base type value copy variable

This is also illustrated by the code:

var num1=2;var num2=num1;

The variable num2 simply copies a copy of the value stored in the NUM1 (2) and then saves it in its own memory space, after which the two variables can participate in any operation without affecting each other.

3.2 Reference type value copy variable

var New  var obj2=obj1; obj1.name= "Jack"; alert (obj2.name); // Jack

Similarly, obj2 copies a copy of the value stored in the Obj1 and saves it in its own memory space, except that the value is actually a pointer to an object stored in the heap. When the copy operation is finished, two variables will actually refer to the same object. Two variables are in fact synchronized with one another.

Iv. Passing parameters

The parameters of all functions in ECMAScript are passed by value: Copy the values outside the function to the parameters inside the function, and copy the values from one variable to another.

4.1 Basic types of values

The passed value is copied to a local variable (that is, a named parameter: the formal parameter, which is an element in the arguments object). See http://www.cnblogs.com/yxField/p/4224380.html mentioned in the.

What to note is:string a special basic data type

In many languages, string is represented as an object, but it is not used in ECMAScript, and string is treated as a basic data type, but it is a very special basic type.

It looks as if a string should be a reference type, but it is not, because it is not an object. Then it looks like it should be the basic data type and should be manipulated by means of a pass-through value.

Look at the following example:

var stra = "This is a string"; var strb = stra;      Stra = "This is another string"; Console.log (STRB); This is a string

As we can see from the above example, it is as if Stra copied a copy to STRB by means of a value transfer. When Stra changes, STRB does not change, and it seems we can already conclude that string is a basic data type.

However, because a string is arbitrary in length and is passed by value, one of the copied bytes is still very inefficient and appears to be a reference type for string.

Look at the following example:

var a = "MyObject";   A.name = "MyName"; Console.log (A.name); Undefined

Shows that a string cannot be treated as an object. In fact, the string in JavaScript cannot be changed, and JavaScript does not provide any method or syntax to change the string.

var a = "MyObject";    A = a.substring (3,5)    console.log (a);//BJ

Remember to do so, there is no change to the string string "MyObject", only a refers to another string "BJ", "MyObject" is recycled.

So it can be said that string does not actually conform to the above two types of data classification. It is a special type that has two attributes that are between two.

4.2 Value of the reference type

When a value of a reference type is passed to a parameter, the address of the value in memory is copied to a local variable, so the change of the local variable will be reflected outside the function.

For example:

1<script type= "Text/javascript" >2         functionsetName (obj) {3Obj.name = "Nicholas";4         }5         6         varperson =NewObject ();7 setName (person);8alert (person.name);//"Nicholas"9 Ten</script>

In the example above, when the variable person is copied to obj, it copies the address of the object it holds in memory to obj, so that obj and the person refer to the same object.

To prove that the object was passed by value, let's look at an example:

1  functionsetName (obj) {2Obj.name = "Nicholas";3obj=NewObject ();4Obj.name= "Jack";5         }6 7         varperson =NewObject ();8 setName (person);9alert (person.name);//"Nicholas"

In the above example, if the person is passed by reference, then Person.name becomes jack, which means that the original reference remains unchanged even if the value of the parameter is modified inside the function. In fact, when you re-write obj inside the function, the variable refers to a local object, and the local object is destroyed immediately after the function has finished executing.

V. Type of Detection

5.1 Detection basic data type: typeof

See http://www.cnblogs.com/yxField/p/4211704.html

5.2 Detection Reference type value: instanceof

For example:

1         varS =[1,2];2         varA =true;3         vari =NewObject ();4         5 6Alert (iinstanceofObject);//true7Alert (AinstanceofObject);//false8Alert (sinstanceofArray);//true

Learn JavaScript from the beginning (eight)--variables

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.