- 1
please anyone explain this example?
function person(name, age) { this.name= name; this.age = age; this.yearOfBirth = bornYear; } function bornYear() {
3 Respuestas
+ 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
+ 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.
0
really,this a good community for learning😊😊😊
thanks a lot☺☺☺☺☺