please anyone explain this example? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

please anyone explain this example?

function person(name, age) { this.name= name; this.age = age; this.yearOfBirth = bornYear; } function bornYear() {

7th May 2017, 9:02 AM
Shroq Khaled Mahdi Al-Nabhani
Shroq Khaled Mahdi Al-Nabhani - avatar
3 Answers
+ 7
The code in the description is incomplete, and the question is too much vague :P Anyway, begin of that code is a function used to create object... a kind of class ( JS doesn't still have such classes concept, but last versions of ES -- ECMA Script, on wich JS is based -- provide some, so in future JS will support them widely -- actually depends of browsers ). It's a way to set an object interface: function person(name, age) { this.name= name; this.age = age; this.yearOfBirth = bornYear; // assign the later declaring function in your example } ... so you can use it as this: var somebody = new person("someone",42); // call the method yearOfBirth of the object 'somebody' of type 'person' // equivalent to bornYear() function call, maybe return the result? var year = somebody.yearOfBirth(); // output the 'age' property of the 'somebody' object of type 'person' alert(somebody.age); // output is 42
7th May 2017, 9:19 AM
visph
visph - avatar
+ 3
@visph ES6 has class concept although this is just a synthetic sugar. under the hood classes are just a function. ES6 classes are not similar to any other programming language classes.
7th May 2017, 11:03 AM
Apel Mahmod
Apel Mahmod - avatar
0
really,this a good community for learning😊😊😊 thanks a lot☺☺☺☺☺
9th May 2017, 8:18 AM
Shroq Khaled Mahdi Al-Nabhani
Shroq Khaled Mahdi Al-Nabhani - avatar