Home > front end > How to understand the internal JS function, the relationship between parameters and object?
How to understand the internal JS function, the relationship between parameters and object?
Time:09-23
, the following code has been very confused, please answer:
The function elegantly-named setName (obj) { Obj. Name="Nicholas". Obj=new Object (); Obj. Name="Greg"; } Var person=new Object (); Elegantly-named setName (person); Alert (person. Name);//output is "Nicholas"
Question: 1, the output is not Greg? 2, how to understand the function of the first obj and the relationship between the second obj? 3, whether can understand elegantly-named setName () function, parameter, obj variable person belong to the global object window the following attributes? They are father, the son of the window is a peer relationship, Obj. Name="Nicholas". Is the parameter, the son of obj global grandson is so? Obj=new Object (); Belong to the functions of sons, so also is the global grandson? Obj. Name="Greg"; Is the grandson, son of So, the second obj is the first to the younger generation of the obj, they are not the same, the same name, let a person misunderstanding, so to understand? 4, I have been confused parameter, how to correctly understand the parameters? Here the parameter of obj with attribute name, the parameter is the object?
CodePudding user response:
This relationship theory, you confuse me, simpler, first obj parameter to pass in the address of the person Object, then the parameter obj. Name assignment, also is a person Object name really be changed, then obj to assign a value to the Object's address, this time with the person it doesn't matter, modify the revision is new out of the Object. The name of the value, with the same person it doesn't matter, Parameter: if the inside is a value type, parameter is a copy of the argument, if pass in is a reference type, then it get's argument reference address, the parameter operation, which is reference to the arguments object, so the argument will also change,
CodePudding user response:
Function elegantly-named setName (obj) {//a room, sitting on the position obj Obj. Name="Nicholas".//a changed named Nicholas Obj=new Object ();//the house a new b and do in the obj position, thus a position left the obj Obj. Name="Greg";//obj position b changed named Greg //so there is a name is Nicholas } Var person=new Object ();//create a a Elegantly-named setName (person);//a into the elegantly-named setName room Alert (person. Name);//nature is Nicholas here
CodePudding user response:
Variable is a reference relationship, you could say that it is a tag Object is A label on something, such as your Courier, what we call A package B package, if I put A package name to B package, then I modify the package B modification is A parcel of original content, and finally to the Courier said that this is your hand A parcel, open on see things changed, and that is the reason why things have always been this stuff, the address has changed, Look at this code: Var person=new Object (); Assume that this is A package Elegantly-named setName (person);//I carry A package into the warehouse to Courier, The express process: Obj//we set a parcel Courier hands called obj Obj. Name="Nicholas".//Courier opened the parcel and found there was a sign, write down the name Nicholas Obj=new Object ();//the parcel Courier hand was replaced by another parcel Obj. Name="Greg";//Courier opened the parcel and found there was a sign, write down the name Greg Courier done I brought my parcel is found in the name of the sign is what?
CodePudding user response:
Obj is equivalent to function within a variable, and into which the person is two things, you send in the js to variable assignment as a reference type, the variable is address,