다음 코드를 함께 사용하면 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);
제한이 없습니다 ... –
은 점이 많이 나온 것처럼 보입니다.하지만 효과가 있다면 적어도 좋습니다. – dandavis