I have doubts about understanding the principles of methods. I understand the functions, and I know that
function hello() {
alert('Hello World');
}
coincides with
var hello = function() {
alert('Hello World');
}
Now what is my problem.
Here is my object with one method. I don’t understand why brackets are not needed yarsLeftinside. function people()
I'm looking for a logical explanation.
function people(name, age) {
this.name = name;
this.age = age;
this.yearsUntilRetire = yearsLeft;
}
function yearsLeft() {
var numYears = 65 - this.age;
return numYears;
}
Create Object
var test = new people('Superman', 50);
test.yearsUntilRetire(); // I understand this code and calling a method in that way
Why can't I write this.yearsUntilRetire() = yearsLeftorthis.yearsUntilRetire = yearsLeft();
source
share