Created
March 9, 2013 08:55
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --- 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