In the code below, is person is an Object or Class or Constructor? What's the difference ?
function Person(name,age) {
this.name = name;
this.age = age;
}
// a function that prints the name of any given person
var printPersonName = function (p) {
console.log(p.name);
};
var bob = new Person("Bob Smith", 30);
printPersonName(bob);
In JavaScript there are no classes. A class is a blueprint of an object. The difference between a class and an object is that a class is just a blueprint not a real physical object.
In a way we could say that in JavaScript functions (such as the constructor) are the closest thing to a class in other languages.
bob is an object. When we assign a variable (such as bob) to a new Person, the operator new reserves memory and propagates the variable bob with the values given as parameters, this is done based on the model provided by the constructor Person.