+ 3
Methods
Why is the output 1994? 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
4 Answers
+ 3
Simple: 2016 - 22 = 1994.š
Explanation: since p is a new person object, you just have to replace the 'this' with 'p'. So p.yearOfBirth() is a method that calls the bornYear function. And the bornYear function returns the value of 2016 - 22.
+ 2
What do you expect? Someone who is 22 in 2016, was born around 1994.
+ 2
Oops, so simple.... ā-ā overlooked the operator
0
as we know this operator give the current location so in the person object we passed 22 and in the function definition born year we returned 2016- this.age where this.age will be substrated from 2016. the value of this.age is 22