JavaScript syntax details--References and replication

Source: Internet
Author: User

Original: JavaScript syntax details--Reference and copy

We all know, JS in the variable assignment there are two ways, recently in the toss themselves to write the tab bar plug-in, encountered a lot of problems usually not noticed. Well, that's clear over there, a little tidying up on the reference and replication-related knowledge, may occasionally add new problems, there are mistakes want to see the person pointed out, to avoid the dissemination of incorrect knowledge.

First of all, the class, reference type: object and its array,date,regexp,function; Basic Package Type: boolean,number,string.

When assigning and passing parameters, a reference type passes a pointer, and the basic wrapper type is to copy the new value pass. The common situation I will not wordy, the main is a few encountered the easy to understand the error situation, as well as the personal conclusion of the inquiry.

Modifying a pointer variable does not necessarily affect the referencing object

It might not be a good idea to understand this, a simple and violent demo will be a good indication of the problem.

var = [1, 2, 3],    = arr1;    
= [4, 5, 6];
Console.log (ARR1); Console.log (ARR2);

This demo is relatively simple, the result is: arr1 for [1, 2, 3] arr2 for [4, 5, 6]

Why the ARR1 has not changed after modifying arr2 = [4, 5, 6] the ARR2, arr2 the pointer variable, and did not touch the original reference array.

The following situation is modified to the original object.

var = [1, 2, 3],    = arr1;    
Arr2.pop ();
Console.log (ARR1); Console.log (ARR2);

The result at this point is: arr1 arr2 equals [1, 2], arr2.pop() equivalent to executing[1, 2, 3].pop(),因此arr2引用的原匿名对象被修改了。

So when you manipulate reference types, be aware that you are modifying the original object. A Little verbose demo:

var    = [1, 2, 3],    = arr1; function Test (a) {    console.log (a);     = [4, 5, 6];    Console.log (a);} Test (ARR2); Console.log (arr1); Console.log (ARR2) ;

The above output is based on the previous test should be

var    = [1, 2, 3],    = arr1; function Test (a) {    //  [1, 2, 3]    a = [4, 5, 6];     // [4, 5, 6]  //  [1, 2, 3]//  [1, 2, 3]

Because when you pass the ARR2 to test, it's the equivalent of execution, a = arr2 so the situation is the same as the first demo. In the same vein, it will be a=[4, 5, 6] changed a.pop() , then the result is like a second demo.

A function to modify its own demo:

var function () {    var a = 1;     function () {        var b = 3;    }}; Test (); Console.log (test);

At this point in the test for var b = 3; this method has a tall name called lazy loading, specifically I will not wordy, you can Baidu or Google two. The use of the function itself is also a reference type characteristics.

The following demo is a reference type in a reference type:

var obj1 = {        arr: [1, 2, 3]    },    == [4, 5, 6];console.log (obj1);

In the output, Obj1 is modified, it is not difficult to understand the execution, the obj2.arr = [4, 5, 6] equivalent obj1.arr = [4, 5, 6] of execution, directly modified the original object. Therefore, a simple assignment operation does not get a copy of an object. If you get a copy, you can for(key in object) traverse the object in a way to copy it.

As for primitive types such as string numbers, the basic types are not written much, those are copy operations, and a new value is obtained.

Welcome to the mistake.

JavaScript syntax details--References and replication

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.