2014-10-08 2 views
0

다음 코드를 함께 사용하면 js 클래스를 만드는 데 필요한 래퍼 함수를 ​​정의하는 가장 좋은 방법인지 궁금합니다.자바 스크립트 OOP 프레임 워크

누구나이 첫 시도로 생각했던 것처럼 더 좋은 예를 공유 할 수 있습니까?

나는 무엇이 없습니까?

j = { 
    context : {} 
}; 

j.namepsace = (function(){ 

    var createnamespace = function(parent, remainder){ 
     if(remainder.length === 0){ 
      return; 
     } 

     parent[remainder[0]] = parent[remainder[0]] || {}; 
     return createnamespace(parent[remainder[0]], remainder.splice(1)); 
    }; 

    return { 
     define : function(ns){ 
      createnamespace(j.context, ns.split('.')); 
     } 
    } 
}()); 

j.class = (function(){ 
    return { 
     construct : function(namespace, name, ctor){ 
      namespace[name] = ctor; 
     }, 
     ctor : function(namespace, name, ctor, prototype){ 
      for (var property in prototype) { 
       if (prototype.hasOwnProperty(property)) { 
        ctor.prototype[property] = prototype[property]; 
       } 
      } 
      namespace[name] = ctor; 
     } 
    } 
}()); 

j.mixin = (function(){ 
    return { 
     with : function(object, objectliteral){ 
      for (var prototype in objectliteral) { 
       if (objectliteral.hasOwnProperty(prototype)) { 
        object[prototype] = objectliteral[prototype]; 
       } 
      } 
     }, 
     toDefintion : function(classdefinition, mixin){ 
      var objectliteral = mixin(); 
      for (var property in objectliteral) { 
       if (objectliteral.hasOwnProperty(property)) { 
        classdefinition.prototype[property] = objectliteral[property]; 
       } 
      } 
     } 
    }; 
}()); 

j.namepsace.define('application.section'); 

j.class.construct(j.context.application.section,'printNameMixin', function(){ 
    return { 
     printName : function(){ 
      console.log(this.name); 
     } 
    }; 
}); 

j.class.construct(j.context.application.section,'person',function(name, address, age){ 
    return { 
     name : name, 
     address : address, 
     age : age 
    }; 
}); 

j.class.ctor(j.context.application.section,'customer',function(name){ 
    this.name = name; 
}, { 
    name : 'unregistered client' 
}); 

j.mixin.toDefintion(j.context.application.section.customer, j.context.application.section.printNameMixin); 

var person = j.context.application.section.person('Blair Davidson','23 Test St',33); 

var cust = new j.context.application.section.customer("Bankwest"); 
cust.printName(); 

console.log(person); 
+0

제한이 없습니다 ... –

+0

은 점이 많이 나온 것처럼 보입니다.하지만 효과가 있다면 적어도 좋습니다. – dandavis

답변

0

귀하의 접근 방식은 흥미 롭다 그러나 당신은 무엇이 잘못되었는지를 요청, 나는 3 가지주의 할 :

1. 프로토 타입 당신의 방법으로

을, 당신은 prototype chain의 메커니즘을 나누기 이는 자바 스크립트의 힘 중 하나입니다.

2. 상속

가 종종 다중 상속을 시뮬레이션하는 데 사용되기 때문에 믹스 인 패턴이 나의 마음에 드는 패턴이 아니다. 그러나 Single Responsability Principle과 같은 책임을 지닌 클래스들 사이의 코드를 인수 분해하기 위해서 상속을 사용해야 만한다는 사실 때문에 좋은 OOP 디자인에서는 다중 상속을 피해야합니다.

당신은 몇 가지 강력한 OOP를 수행 할 경우

3. OOP, 당신은 확실히 (인터페이스, 의존성 주입, 같은 ...)보다 더 많은 기능이 필요합니다. 이를 위해, 필자는 기존의 프레임 워크를 살펴보기를 제안한다. 내 프로필의 링크를 따라 가면서 그런 종류의 프레임 워크 예제를 볼 수 있습니다.이 프레임 워크는 nodejs에서만 작동하는 자바 스크립트 프레임 워크이지만 클라이언트 (브라우저)와 서버 (nodejs) 측면에서 모두 사용할 수 있습니다.

관련 문제