Skip to content

Instantly share code, notes, and snippets.

@Sljubura
Created March 1, 2013 12:48
Show Gist options
  • Save Sljubura/5064433 to your computer and use it in GitHub Desktop.
Save Sljubura/5064433 to your computer and use it in GitHub Desktop.
Inherit both the properties and methods from parent.
// 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