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,

CodePudding user response:

reference 1/f, henan good response:
this relationship theory, you make me confused, the simpler, the 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,


Thank you! I see, originally I have been under the illusion that function two obj is different: one is the parameter obj, another is a variable obj,
Obj is the original function parameter obj, just by pointing to twice, the second obj pointed to a new object, whether parameter accepts arguments passed in, can use the code so understanding:
Parameter code obj is replaced by the person? As shown in the following:
 function elegantly-named setName (person) {
Person. Name="Nicholas".
The person=new Object ();
Person. Name="Greg";
}

CodePudding user response:

refer to the second floor similing 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);//here is naturally Nicholas


Thank you, describe very lively! But one thing has not been very understand:
Var person=new Object ();//create a a
Since created earlier, why not put this sentence in the function (after all, created in the former) before? Why are used in the function (a) entering the back?
The logical order seemed to violate conventional thinking, so I have been to functions and objects can be confusing,
Seek to reassure ~ ~

CodePudding user response:

reference similing reply: 3/f
variable is a reference relationship, you can say 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?


You mean, within the function parameter obj is just a label, but the parcel (object) has changed, just tag obj remained unchanged, so to understand? When a person as a label to go in, obj this tag is replaced with the person, the function of the second parcel, also with the person tag?

CodePudding user response:

This Object labeled the person first, and then passed to the parameters of labeled the obj label again, after setting the name obj pasted the label was torn off on another Object, outside the function Object is person labels, so use person reference name can see Nicholas
  • Related