2012-10-07 5 views
3

OO 접근 방식을 Java Script에서 사용할 수 있습니까? JavaScript를 에서 서버 쪽과 클라이언트 쪽 모두 node.j를 사용하여 사용합니다. 현재 쿼리 대신 CRUD 작업에 쿼리를 사용하고 있습니다. DTO의을 사용하여 데이터베이스에 데이터를 저장 하시겠습니까?Java Script에서 객체 지향 접근 방식

+0

더글러스 크록 포드의 * 자바 스크립트를 읽어 보시기 바랍니다 :. 좋은 부품 " – ebohlman

+0

확인 ebohiman 내가 시도 할 것이다 – Baskar

답변

0

예, 프로토 타입 상속을 사용하여 에뮬레이트 할 수 있습니다. 규칙은 언어가 프로토 타입을 기반으로하므로 프로토 타입 체인과 관련하여 약간의 연구가 필요할 것입니다. 그러나 결국에는 매우 유용합니다.

EventEmitter에서 상속받은 Node 코어의 것들을 확인하십시오. 그들은 util.inherits라는 내장 함수를 가지고 있으며,이 함수는 최신 버전의 ECMA를 구현합니다. 그것은 다음과 같습니다

https://github.com/joyent/node/blob/master/lib/stream.js#L22-29 커피 스크립트에서

var events = require('events'); 
var util = require('util'); 

function Stream() { 
    events.EventEmitter.call(this); 
} 
util.inherits(Stream, events.EventEmitter); 

, 수업이 __extends 귀결 코드의 약간 다른 세트로 컴파일 :

/** 
* Inherit the prototype methods from one constructor into another. 
* 
* The Function.prototype.inherits from lang.js rewritten as a standalone 
* function (not on Function.prototype). NOTE: If this file is to be loaded 
* during bootstrapping this function needs to be rewritten using some native 
* functions as prototype setup using normal JavaScript does not work as 
* expected during bootstrapping (see mirror.js in r114903). 
* 
* @param {function} ctor Constructor function which needs to inherit the 
*  prototype. 
* @param {function} superCtor Constructor function to inherit prototype from. 
*/ 
exports.inherits = function(ctor, superCtor) { 
    ctor.super_ = superCtor; 
    ctor.prototype = Object.create(superCtor.prototype, { 
    constructor: { 
     value: ctor, 
     enumerable: false, 
     writable: true, 
     configurable: true 
    } 
    }); 
}; 

예제 사용 스트림 클래스입니다 기능. I 이것은 브라우저 간 호환성이 더 높을 것이라고 생각하지만 특히 Object.create를 지원하지 않는 사람을 구체적으로 기억하지 않습니다.

var __hasProp = Object.prototype.hasOwnProperty, __extends = 
function(child, parent) { 
    for (var key in parent) { if (__hasProp.call(parent, key)) 
child[key] = parent[key]; } 
    function ctor() { this.constructor = child; } 
    ctor.prototype = parent.prototype; 
    child.prototype = new ctor; 
    child.__super__ = parent.prototype; 
    return child; 
    }; 
+0

확인 조쉬 내가 시도 할 것이다. – Baskar

관련 문제