Skip to content

Instantly share code, notes, and snippets.

@RodolfoSilva
Forked from badsyntax/object_create.js
Created April 29, 2016 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RodolfoSilva/9f64718d6bb9d9b10026885edf50fcc6 to your computer and use it in GitHub Desktop.
Save RodolfoSilva/9f64718d6bb9d9b10026885edf50fcc6 to your computer and use it in GitHub Desktop.
Simple prototypal inheritance in node.js
/* 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