JavaScript assignment is referencing or copying, and parameter passing

Source: Internet
Author: User

  First look at the 2014 Alibaba front-end online written question:

  var a = 1; var obj = {    b:2}; var fn = function () {}; FN.C = 3;   function test (x, Y, z) {    x = 4;     y.b = 5;     Z.C = 6;     return z; Test (A, obj, FN); Alert (A + obj.b + fn.c); The answer is 12, if the answer is correct, there is no need to watch. If not, just come with me to study.   (a) basic types   Since learning JS not long, whether the assignment is a reference or a copy is not very clear (if it is a reference, that one value change will affect the other, if it is copied, that each play each, do not affect), It is often confusing to encounter such problems. Spent half a day to read some of the information, sorted as follows.   First of all, the basic types of JavaScript are boolean,string,number, and undefined and null, ah, you will say why do you pull the most boring book of the first chapter of the grammar, understand this and what, really useful. The first thing to understand is that only literal boolean,string and number, and undefined+null (exact undefined null, case-sensitive) are the basic types, and new ones are not counted.   i.e.:   alert (typeof false);//"Boolean" var B = new Boolean (false); Alert (typeof B);//"Object" Likewise, new String (' AAA ') is not a basic type, direct a= ' AAA ', A is the basic type, that is: literal is the basic type.   On the other hand, Object,function,array are actually constructors, because they can be directly new Object (), so they are all functions, so (Object instanceof function = = True) && amp; (Function instanceof Object===true).   Note that basic types do not have properties and methods, but they can invoke methods that correspond to the basic wrapper type.Take a look at the following example:   var a = ' ot '; A.age = 18;//hahahahaha,yeah,forever alert (a.age);//undefined a.length = 160; alert (a.length);//2 The second step of a.age = 18 is actually divided into three steps when implementing:   var S1 = new String (' ot '); S1.age = 18; S1 = null; That is, the base type creates a new object each time the type or method is invoked, and then destroys it.   Similarly, at alert (a.age), the following steps are divided:   var s2 = new String (' ot '); alert (s2.age); S2=null; Because the age method is not defined on S2, the output is undefined. At alert (a.length), the string has a length method because of the s3=new string (' ot '), so the result is output, and of course the object that was assigned to the previous step must have been destroyed.   (b) After referring to the basic type of or Copy   clear, to understand that the base type variable exists inside the stack, each assignment creates a new Copy, and then play with itself. In addition to a reference type other than the base type, there is heap memory, which can only be referenced. Let's take a look at the following example:   var ot = new Object ()///create a target that assigns the address to OT, that is, OT points to this address var op = ot;//The value of OT to OP, so op is also pointing to that object op.age = 18; alert (ot.age);//18   op = new String (' sunshine ');//Assign the address of the newly created object to Op,ot certainly unchanged alert (ot.length);//undefined read the annotation section, I believe I can understand it already.   (c) parameter pass   JavaScript parameter pass for value delivery, let's look at the following example:   function Setage (i) {    alert (i);//24     i = 18;     alert (i);//18};  var ot = 24; Setage (OT);   alert (OT);//24 the value of OT 24 in, assigned to I, so the first alert is 24, then I reassign, so alert comes out of 18, but the outer ot is not affected, because the transfer value, that is, copy the content to I.   What happens when the value passed is a reference type? First look at the example:   function SetName (obj) {    obj.name = ' ot ';};   var obj2 = new Object (); SetName (OBJ2); alert (obj2.name);//ot Copy code this looks like a quote, because Obj.name is changed, but it's not, it's actually a value, because Obj2 's own value is the address of the new object, so that's the address.   (iv) Back to face questions   We now look at the previous interview question:   var a = 1; var obj = {    b:2}; var fn = function () {}; FN.C = 3;   function test (x, Y, z) {    x = 4;     y.b = 5;     Z.C = 6;     return z; Test (A, obj, FN); Alert (A + obj.b + fn.c); First, in the argument passed in by Test, A is the base type (ah yes, copy a value, OH), obj is object (point to address shout, you move me too), FN is certainly not the basic type. When the test is executed, X is assigned a value of 4 (it's OK with a, play each one, A is still 1), and Y's B is assigned 5, and the B of obj becomes 5,z C to 6, and the FN C of course is 6. So the result of alert should be 1+5+6 =12. (In fact, Test does not return z as well, z still changes).
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.