Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Created April 4, 2014 07:11
Show Gist options
  • Save badsyntax/9969617 to your computer and use it in GitHub Desktop.
Save badsyntax/9969617 to your computer and use it in GitHub Desktop.
Broken extends in Node.js
var Base = module.exports = function(db) {
this.db = db;
}
Base.prototype.extend = function(properties) {
var Child = Base;
Child.prototype = Base.prototype;
for(var key in properties) {
Child.prototype[key] = properties[key];
}
return Child;
};
var Model = require('./BaseModel');
var model = new Model();
var BlogModel = model.extend({
results: 10,
getContent: function() {
return "blog content, results: " + this.results;
}
});
var blogModel = new BlogModel();
var ContactsModel = model.extend({
results: 50,
getContent: function() {
return "contacts content, results: " + this.results;
}
});
var contactsModel = new ContactsModel();
console.log(blogModel.getContent()); // contacts content, results: 50
console.log(contactsModel.getContent()); // contacts content, results: 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment