Introduction to reference and replication in js (passing values and transferring addresses) and introduction to js

Source: Internet
Author: User

Introduction to reference and replication in js (passing values and transferring addresses) and introduction to js

It seems that few people talk about references and replication in js, but figuring out this concept can help you understand a lot.

First, let's talk about basic things and see what the data types in js are uploaded separately.

References: objects, arrays, and functions

Copy: Number, Boolean

Because of its special nature, it cannot be determined whether to pass the reference or copy the value (because the value of the string cannot be changed, it is meaningless to tangle with this problem) however, when used for comparison, it is obviously a comparison of pass-through values (the comparison will be described later)

The following describes how to use it.

The most common use is the assignment.

Var a = 1; var B = a; // The copy value of a is B ++; alert (); // modification of "1" B does not affect /**************************** * ***********/var a = [1]; var B = a; // assign a reference to B [0] ++; alert (); // modification of "2" B is also valid for a, but of course B = [2]; this modification is useless for.

Function Parameters

Transfer of values: the data is transferred to the function as a copy of the value, which is invisible to the function for its modifications.

Var a = 1; var B = 2; function change (a, B) {var c = a; a = B; // overwrite B = c with a new reference; alert (a); // "2" alert (B); // "1"} change (a, B); alert (); // "1" alert (B); // "2"

Address Transfer:It is a reference of a value that is passed to the function. The modification to its attribute is externally visible, but it is invisible if it is overwritten by a new reference. For example

Var a = [1, 2, 3]; var B = [5, 6]; function change (a, B) {a [0] = 4; // externally visible var c = a; a = B; // overwrite B = c with a new reference; alert (); // "" alert (B); // ", 3"} change (a, B); alert (, 3 "alert (B); //" 5, 6"

From the results, we can see that a and B are not interchangeable, because they are not visible to the outside with the new reference. This is natural because the function only gets the reference and has no right to change the reference.

The following is different.

var a = [1, 2, 3];var b = [5, 6];function change() { var c = a; a[0] = 4; a = b; b = c;};change();alert(a);  //"5,6"alert(b);  //"4,2,3"

SWAps are successfully implemented here

We must also mention the block-level scope of js. in some languages, it is definitely to report an undefined error. Because js does not have a block-level scope, it cannot find variable a in change, B will consciously go to the upper layer, so here a and B are references to global variables.

The above a and B are the variables in the change function. When the function is called, the references of a and B are passed to these two variables, but the global a cannot be changed, b. I think it's better to change the name here.

This is a bit of a question ....

Back to references and replication considerations in comparison

The comparison of pass-through values is numeric, and the comparison of transfer-through values is reference. Different references may vary even if the values are the same.

1 == 1;  //true1 === 1;  //true[0] == [0]; //false[0][0] == [0][0];  //true[0][0] === [0][0];  //true[0].toString() == [0].toString();  //true 

Closure ....

Closures are probably the most tangled things in js. The typical questions we interview in our department are always tested ....

Here, I will not talk about the closure, but only about the part that involves passing values and references, when I decide one day that I can use clear regulations, concise language, and vivid examples to thoroughly explain that thing, I will introduce in detail this guy who cannot but mention js...

In the closure, internal functions use local variables of external functions as references instead of copies.

In fact, this is also a very important part of understanding closures. We can use this to explain a classic closure phenomenon. In many cases, we will use this example to illustrate closures.

/* Construct a function to set an event handler for the nodes in the array. When you click a node, alert returns the node serial number */var add_handlers = function (nodes) {var I; for (I = 0, l = nodes. length; I <l; I ++) {nodes [I]. onclick = function (e) {alert (I); // Of course, The result here is the total number of nodes in each alert operation .... }}};

Why is the total number of nodes rather than the expected sequence number used for each alert operation? It would be quite easy to explain it by means of replication and reference.

Because internal functions use the reference instead of copying when using external variables, that is, when I set an onclick event for each node, I passed the reference to alert, when I click a node to trigger the onclick event, the value of I has changed to the total number of nodes...

var add_handlers = function (nodes) {  var i;  for (i = 0, l = nodes.length; i < l; i ++) {     nodes[i].onclick = function (i) {      return function(){      alert(i);        } }(i);  }};

The reason for this modification is that the copy of the I value is passed in at this time. In fact, it is the same as that of a common function. Do not confuse it with the closure, back to the source, you will understand that the source is the transfer of the address mentioned above.

By the way, don't be caught by the strange name of the closure. In fact, it works the same way as the function we usually use, aside from the so-called features of closures such as "a longer life cycle" and "protecting private variables, think of it as a common function (you can also think of a global function as a special closure from another angle), which is easy to understand.

The key is to let go of all the flashy things and return to the essence... I ran the question again...

The reference and replication (value transfer and address transfer) in js is all the content shared by Alibaba Cloud xiaobian. I hope you can provide a reference and support for the customer's home.

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.