JavaScript Intensive Tutorial--object value passing and reference passing

Source: Internet
Author: User

This article ish5eduAgency officialHTML5 Trainingtutorials, the main introduction:JavaScript Intensive Tutorials--Object value passing and reference passing
 

Function setname (obj) { obj.name= "Tom";//Before execution, the Name property of obj and person at this time is undefined obj1=new  Object ();  obj1=obj;//declares a global object, then obj, obj1, and person should be the same object at this time  }//setname function is executed, the Obj object is destroyed and the remaining objects are still present   Person=new object ();//declares the person object  setname (person);//invokes the SetName function and passes the object as a parameter  obj1.name= "Lucy";// Change the Name property of person by Obj1  alert (person.name);//Output "Lucy", the left side of the code conclusion gives the impression that the JavaScript object is passed by reference/* I want to know when calling SetName ( person), whether the person passes to the function setname (obj) position is either a value pass or a reference pass, and if you follow the instructions--excerpt to the JavaScript Advanced Tutorial (third edition) function  setname (obj) {obj.name= "Tom"; Obj=new object (); Obj.name= "Lucy";} Person=new object ();//Declare Person object SetName (person);//alert (person.name);//Output Tom, I feel that the person and obj should be using a reference when calling the function. But to re-declare the obj local object in the function, I think after executing the obj=new object () re-declaring the object, obj and the person object calling SetName (person) are different references (addresses) ,  obj.name= "Lucy", this sentence to person at this time does not work at all, so there will be the conclusion of the book, the output as "Tom" results, so I think the book is still not enough to prove that the object in JavaScript is the value of the transfer of this conclusion , I would like to ask how to understand this problem, thank you, * * again attached to the Internet to find a piece of code, in the call function input is the object, why have different results: &LT;SCRIpt type= "Text/javascript" >function foo (v3) {V3={a:3};alert (v3.a);//3}var v4={};foo (v4); alert (v4.a);//undefined</script><script type= "Text/javascript" >function fooled (v2) {v2.b=4; alert (v2.b);//4}var v1=new object (); fooled (v1); alert (v1.b);//4</script>

Click to enter JS Intensive tutorial

This article is from the "11721999" blog, please be sure to keep this source http://11731999.blog.51cto.com/11721999/1831562

JavaScript Intensive Tutorial--object value passing and reference passing

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.