Created
May 8, 2010 15:45
-
-
Save jeena/394616 to your computer and use it in GitHub Desktop.
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
// Record Object | |
function Record(id) { | |
this.id = id; | |
} | |
Record.prototype.save = function() { | |
console.log("saving record " + this.id); | |
} | |
Record.prototype.delete = function() { | |
console.log("deleting record " + this.id); | |
} | |
// User Object | |
function User(id) { | |
this.constructor(id) | |
}; | |
// Inheritance | |
User.prototype = new Record(); | |
// Save a link to the parent class | |
User.prototype.parent = Record.prototype; | |
// Adding method | |
User.prototype.test = function() { | |
console.log("testing user" + this.id); | |
} | |
// Overwriting a method | |
User.prototype.delete = function() { | |
console.log("deleting user " + this.id); | |
// Calling a overwritten parent method | |
this.parent.delete.call(this); | |
} | |
// Test | |
var aUser = new User(22); | |
aUser.save(); | |
aUser.test(); | |
console.log(aUser.id); | |
aUser.delete(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Record Object
function Record(id) {
this.id = id;
}
Record.prototype.save = function() {
console.log("saving record " + this.id);
}
Record.prototype.delete = function() {
console.log("deleting record " + this.id);
}
// User Object
function User(id) {
this.constructor(id)
};
// Inheritance
User.prototype = new Record();
// Adding method
User.prototype.test = function() {
console.log("testing user" + this.id);
}
// Overwriting a method
User.prototype.delete = function() {
console.log("deleting user " + this.id);
}
// Test
var aUser = new User(22);
aUser.save();
aUser.test();
console.log(aUser.id);
aUser.delete();