2012-02-05 4 views
0

나는 밑줄과 Mongodb를 사용하여 Javascript에서 Rails ActiveRecord의 유사한 버전을 빌드하려고합니다. 새로 생성 된 객체가 클래스 생성자로부터 그의 프로토 타입을 상속받을 수있는 방법에 관해서는 머리를 감쌀 수없는 무언가가 있습니다. 어쩌면 나는 나의 점을 설명하는 경우가 쉬울 것 :자바 스크립트 생성자 프로토 타입

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); 

개체 하나의 프로토 타입이 Database.ActiveRecord.prototype을 상속하지 않습니다. 무엇이 문제일까요?

답변

1

개체 인스턴스에서 프로토 타입은 constructor.prototype 속성을 통해 액세스 할 수 있습니다.

따라서, one.constructor.prototype === Client.prototype.

잘못된 속성을 확인하는 것 같습니다. 이 아니고 one.prototype이되어야합니다.

또한 인스턴스 개체의 __proto__ 속성을 살펴보십시오.

+0

이것은 서버 측 내용입니다. 그것을 지정 했어야합니다. – mabounassif

+0

당신은 실제로 옳습니다, 제가 틀린 프로토 타입이라고 부르는 방식입니다. 속성이 있습니다. – mabounassif

관련 문제