Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hamidreza-s/5123545 to your computer and use it in GitHub Desktop.
Save hamidreza-s/5123545 to your computer and use it in GitHub Desktop.
Object.create is new to JavaScript (it's part of ES5), but NodeJS supports it so we can safely use it. This creates a new object that inherits from another object.
// --- Classical OOP
function Person(name) {
this.name = name
}
Person.prototype = {
greet: function () {
return "Hello world, my name is " + this.name;
}
};
var frank = new Person("Frank Dijon");
frank.greet();
// --- Prototypal OOP
var Person = {
greet: function () {
return "Hello world, my name is " + this.name;
}
};
var frank = Object.create(Person);
frank.name = "Frank Dijon";
frank.greet();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment