I’m trying to build a similar version of Rails ActiveRecord in Javascript, using underscore and Mongodb. There is something that I can’t wrap my head around concerning the way a newly created object can inherit his prototype from the constructor of the class. Maybe if I illustrate my point it would be easier:
var root = this;
var Database = root.Database = {};
// Require Underscore, if we're on the server, and it's not already present.
var _ = root._;
if (!_ && (typeof require !== 'undefined')) _ = require('./underscore');
Database.ActiveRecord = function(attributes){
attributes || (attributes = {});
this.attributes = {};
};
_.extend(Database.ActiveRecord.prototype, {
idAttribute: '_id',
test : 1,
});
var Client = Database.ActiveRecord;
var one = new Client();
console.log(one.prototype);
The object one’s prototype doesn’t inherit the Database.ActiveRecord.prototype. What might be the problem?
From an object instance, the prototype is accessible via the
constructor.prototypeproperty.So,
one.constructor.prototype === Client.prototype.It seems your are just checking the wrong property, should be
one.constructor.prototype, notone.prototype.Also have a look at the
__proto__property of an instance object.