Discussion about better understanding of function


#1

function Person(name,age) {
this.name = name;
this.age = age;
this.species = "Homo Sapiens";
}

var sally = new Person("Sally Bowles" , 39);
var holden = new Person("Holden Caulfield" , 16);
console.log("sally's species is " + sally.species + " and she is " + sally.age );
console.log("holden's species is " + holden.species + " and he is " + holden.age );

I understand the code but can any one can help me to know how function is actually playing role here in depth .


#2

These "functions"are actually javscript objects so you do not call them like regular functions but you create instances of the object like you've done here,

As you can see the object's parameters must correlate with the object's function or class,

function Person(name,age) {
this.name = name;
this.age = age;
this.species = "Homo Sapiens";
}

#3

Very nice answer @bandit
Additionally, I explained some stuff about functions in JavaScript yesterday in the topic:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.