Methods | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

6th Feb 2018, 3:43 AM
Timothy Allen Johnson II
Timothy Allen Johnson II - avatar
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.
6th Feb 2018, 4:43 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 2
What do you expect? Someone who is 22 in 2016, was born around 1994.
6th Feb 2018, 4:33 AM
1of3
1of3 - avatar
+ 2
Oops, so simple.... “-“ overlooked the operator
6th Feb 2018, 4:59 AM
Timothy Allen Johnson II
Timothy Allen Johnson II - avatar
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
7th Feb 2018, 5:57 AM
Rohit Goyal
Rohit Goyal - avatar