Skip to content

Instantly share code, notes, and snippets.

@FaisalAl-Tameemi
Created July 27, 2016 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FaisalAl-Tameemi/d1e260ef508962f2365243b77df15386 to your computer and use it in GitHub Desktop.
Save FaisalAl-Tameemi/d1e260ef508962f2365243b77df15386 to your computer and use it in GitHub Desktop.

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);
@FaisalAl-Tameemi
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment