-
-
Save th3hunt/23323357345b717b0d90 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
/* This shows how to use Object.create */ | |
/** BASE MODEL **********************************/ | |
function BaseModel(collection) { | |
this.collection = collection; | |
} | |
BaseModel.prototype.getCollection = function() { | |
return this.collection; | |
}; | |
/** User Model ***********************************/ | |
function UserModel() { | |
// Call the super class' constructor, in the context of this UserModel instance, | |
// passing in a parameter. | |
BaseModel.call(this, 'users'); | |
} | |
// Inherits the prototype methods from the base model. | |
UserModel.prototype = Object.create(BaseModel.prototype); | |
UserModel.prototype.constructor = UserModel; | |
UserModel.prototype.delete = function() { | |
return 'delete a user in collection ' + this.getCollection(); | |
}; | |
/** Blog Model ***********************************/ | |
function BlogModel() { | |
BaseModel.call(this, 'blog'); | |
} | |
// Inherits the prototype methods from the base model. | |
BlogModel.prototype = Object.create(BaseModel.prototype); | |
BlogModel.prototype.constructor = BlogModel; | |
BlogModel.prototype.getPosts = function() { | |
return 'posts in collection ' + this.collection; | |
}; | |
/*** Example *************************************/ | |
var person = new UserModel(); | |
console.log(person.delete()); // 'delete a user in collection users' | |
var blog = new BlogModel(); | |
console.log(blog.getPosts()); // 'posts in collection blog' |
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
/* This shows how to use util.inherits */ | |
var util = require('util'); | |
/** BASE MODEL **********************************/ | |
function BaseModel(collection) { | |
this.collection = collection; | |
} | |
BaseModel.prototype.getCollection = function() { | |
return this.collection; | |
}; | |
/** User Model ***********************************/ | |
function UserModel() { | |
// Call the super class' constructor, in the context of this UserModel instance, | |
// passing in a parameter. | |
BaseModel.call(this, 'users'); | |
} | |
// Inherits the prototype methods from the base model. | |
util.inherits(UserModel, BaseModel); | |
UserModel.prototype.delete = function() { | |
return 'delete a user in collection ' + this.getCollection(); | |
}; | |
/** Blog Model ***********************************/ | |
function BlogModel() { | |
BaseModel.call(this, 'blog'); | |
} | |
util.inherits(BlogModel, BaseModel); | |
BlogModel.prototype.getPosts = function() { | |
return 'posts in collection ' + this.collection; | |
}; | |
/*** Example *************************************/ | |
var person = new UserModel(); | |
console.log(person.delete()); // 'delete a user in collection users' | |
var blog = new BlogModel(); | |
console.log(blog.getPosts()); // 'posts in collection blog' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment