I’m having a bit of trouble understanding what the assignment operator is used for when dealing with methods, functions, etc. Here is the example in w3 school for defining an object
function person(firstname,lastname,age,eyecolor){
this.firstname=firstname;
this.eyecolor=eyecolor;
this.newlastname=newlastname;
}
and this is the actual function (place somewhere else)
function newlastname(new_lastname){
this.lastname=new_lastname;
}
It’s just very weird to me throughout javascript, you say
object.methodname = somefunctionname
Any ideas to help me conceptualize it?
The code in your question is effectively the same as this:
personis a constructor function (you would call it with thenewoperator to create a new instance). Every instance ofpersonhas three properties,firstname,eyecolorandnewlastname.The
newlastnameproperty is a method since it’s value is a function. When you call that method, the instance ofpersonon which it is called will get alastnameproperty.For example:
This is possible because in JavaScript, functions are objects.