Javascript构造函数原型
我正在尝试用Javascript构建一个类似的Rails ActiveRecord版本,使用下划线和Mongodb。关于一个新创建的对象如何从类的构造函数继承他的原型,有一点我无法理解。如果我能说明我的观点,可能会更容易:Javascript构造函数原型,javascript,mongodb,underscore.js,Javascript,Mongodb,Underscore.js,我正在尝试用Javascript构建一个类似的Rails ActiveRecord版本,使用下划线和Mongodb。关于一个新创建的对象如何从类的构造函数继承他的原型,有一点我无法理解。如果我能说明我的观点,可能会更容易: var root = this; var Database = root.Database = {}; // Require Underscore, if we're on the server, and it's not already present. var _ =
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);
对象1的原型不继承Database.ActiveRecord.prototype。可能有什么问题?从对象实例,可以通过
构造函数.prototype
属性访问原型
所以,one.constructor.prototype==Client.prototype
似乎您只是在检查错误的属性,应该是one.constructor.prototype
,而不是one.prototype
还可以查看实例对象的属性。这是服务器端的东西。你是对的,我对原型的称呼是错误的。酒店就在那里。