Created
March 1, 2013 12:48
-
-
Save Sljubura/5064433 to your computer and use it in GitHub Desktop.
Inherit both the properties and methods from parent.
This file contains 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
// Pattern to inherit both the properties and methods | |
function Parent(name) { | |
this.name = name || "Vladimir"; | |
} | |
Parent.prototype.say = function () { | |
return this.name; | |
}; | |
function Child(name) { | |
Parent.apply(this, arguments); | |
} | |
Child.prototype = new Parent(); | |
var kid = new Child("Wesley"); | |
console.log(kid.name); // Wesley | |
console.log(kid.say()); // Wesley | |
delete kid.name; // Note that you can only delete properties, not variables | |
console.log(kid.say()); // Vladimir, gets propertie form parent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment