The basics of JavaScript are deeply

Source: Internet
Author: User

JS's numeric types are divided into two categories: the base data type and the reference data type.

The base type occupies the memory stack space, and the reference type is saved in the heap space. A variable of reference type assignment is also stored in the stack space, which acts like a TV remote control, and is responsible for manipulating the objects pointed to in the heap space.

var num = 10;var change = function (obj) {      obj = obj+10;      return obj;} var result = change (num); Console.log (Result)//20console.log (num)//10

The above example passes num as a parameter to the function change, but the operation on the parameter obj does not affect the value of num, because the parameter is passed by value, and there is no doubt about the basic data type.

var a = {value: "teacher"};var b = a;b.value = "Student"; Console.log (a);//{value: "Student"}b = 2;console.log (a);//{value: "Student"}

Looking at the code above, the a variable points to an object, assigns a to the B variable, and a and B point to a memory address, so both A and B can operate on the object {value: "Teacher"}. When the value of B is modified to "student", the value of the A variable also changes because they manipulate a memory address. But when we assign a value of 2 to B, the A variable does not change. With this problem, we look at the transfer of JS function parameters.

var object = new Object (), var change = function (obj) {      obj.name = "Jim";} Change (object); Console.log (object);//{name: "Jim"}

You can see that the effect is consistent with what we expected, or that the variable object and the parameter obj point to a piece of memory, and changing obj changes the object. But this example gives us an illusion that parameters are passed by reference. JS Red Book in the face of this has to explain, whether it is a basic type or reference type, parameters are value passing. Referring to the previous example, we are writing a function that changes the parameters of the reference point to the demonstration.

var object = new Object (), var change = function (obj) {      obj.name = "Jim";      obj = new Object ();      Obj.name = "Carry";} Change (object); Console.log (object);//{name: "Jim"}

The variable object does not change with the subsequent changes to the parameter obj. We may as well as the author of the Red Book in-depth projections, the individual think can be divided into two levels to understand the transmission of information:

    Level 1: As long as it's inside the object, we can assume that this is a reference, the memory address is equivalent to the object variable and the parameter obj connection bridge, no matter who operates it, the other will change.

Layer 2: When the reference address of any one variable changes, that is, the parameter obj points to the other memory address, which can be understood as a value pass. I believe the author of the Book of Red paper is saying so. The connection between the variable object and the parameter obj is broken, and the natural parameter obj does not have an effect on the object variable. Also, the new memory address that the local variable obj points to will be reclaimed by the GC as soon as the function call is complete.

Relative to the red Book author's argument by the value of the interpretation, netizens gave a view, that is, between the value of the transfer and reference delivery there is a shared pass.

In fact, shared delivery is just a term that you can think of as a reference, or as a value, or as a share.

But rather than the term, we should pay more attention to its internal principle, that is, under what circumstances two variables will affect each other, under what circumstances will not affect.

The basics of JavaScript are deeply

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.