Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Created April 4, 2014 08:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save badsyntax/9970212 to your computer and use it in GitHub Desktop.
Save badsyntax/9970212 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'
@dmitrykvochkin
Copy link

Thank you very much mate! I really appreciate it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment