+ 2

Passing parameter to function when it is assigned to an object

is it possible to pass a parameter to function when it is assigned to an object? eg: function person(name, age) { this.name= name; this.age = age; this.yearOfBirth = bornYear; } function bornYear(parameter) { return 2016 - this.age; } if yes, how to access it?

11th Mar 2017, 8:49 AM
Chahat Gupta
Chahat Gupta - avatar
1 Answer
+ 3
I'm not sure I fully understand your question but if you want to add a method that accepts parameters, to an object you can follow the example below. function myObj(name){ this.name= name; } myObj.prototype.getYearOfBirth = function(param){ var operation = 2017 - param; return operation; } var person = new myObj("seamiki"); alert(person.name + " was born in " + person.getYearOfBirth(60));
11th Mar 2017, 9:23 AM
seamiki
seamiki - avatar