Skip to content

Instantly share code, notes, and snippets.

@th3hunt
Forked from badsyntax/object_create.js
Created August 31, 2014 14:32
Show Gist options
  • Save th3hunt/23323357345b717b0d90 to your computer and use it in GitHub Desktop.
Save th3hunt/23323357345b717b0d90 to your computer and use it in GitHub Desktop.
/* 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 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