+ 1
js objects
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 how can you a call a property name using the function call operator genarlly we call the property of the object using syntax objectname.propertyname but here it is not the case plz help
3 Answers
+ 4
With this keyword, it is a pre-ES6 syntax of defining a class with functional constructor.
In the constructor,
this.yearOfBirth is assigned bornYear,
where bornYear is declared as a function and hoisted.
So yearOfBirth is a class prototypical method.
So it is called with
classname.methodname()
here is an edit logging the type:
https://code.sololearn.com/WLk7WaIKawe7/?ref=app
+ 3
Please elaborate rather than reposting a question.
https://www.sololearn.com/Discuss/2250672/?ref=app
+ 2
A little extra information that may also help.
In JavaScript, functions are first-class objects, which means they can be: stored in a variable, object, or array. passed as an argument to a function. returned from a function
https://medium.com/launch-school/javascript-weekly-an-introduction-to-first-class-functions-9d069e6fb137