Adding method part 3

JavaScript Tutorial
Objects
Adding Methods
59
3/3
               

Methods



Call the method as usual.
function person(name, age) {
this.name= name;
this.age = age;
this.yearOfBirth = bornYear;
}
function bornYear() {
return 2016 - this.age;
}

var p = new person("A", 22);
document.write(p.yearOfBirth());
// Outputs 1994
Try It Yourself

Call the method by the property name you specified in the constructor function, rather than the function name.

Comments

Popular posts from this blog

String Operators

Introducing object part 3

The math object part 2