In the following function, with a method inside of it called newlastname :
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
this.newlastname=newlastname;
}
function newlastname(new_lastname)
{
this.lastname=new_lastname;
}
In the line this.newlastname=newlastname; what is happening? What does the first newlastname refer to? I appreciate any tips or advice.
In this line of code:
The first
newlastnameis a property on thepersonobject.The second
newlastnameis a reference to thenewlastname()function.So, when you do this:
you are storing a reference to that function in a property of the
personobject. That would allow the following code to work:When you execute
p.newlastname("Bundy");, it will look for a property on thepersonobject callednewlastname. When it finds that property, it will execute that function and pass it"Bundy"and setthisto be the particularpersonobject.