0
인스턴스 개체의 개인 상태는 일반적으로 권장되지 않으며 다음 구현의 결함/단점을 지적 해 주시면 감사하겠습니다. 조언/비평은 크게 감사드립니다.인스턴스 개체의 개인 상태 구현 다음과 같은 결함
var namespace = {};
namespace.parent = {
parent_method1 : function() {},
parent_method2 : function() {}
};
namespace.child = function (properties) {
var private="secret";
this.prototype = {
create : function() {
this.unique = 'base';
this.unique += this.properties;
return this.unique;
},
get_private: function() {
console.log(private);
},
set_private: function (val) {
private = val;
}
};
var proto = Object.create(namespace.parent);
var instance = Object.create(proto);
for (var property in this.prototype) {
if (this.prototype.hasOwnProperty(property)) {
proto[property] = this.prototype[property];
}
}
instance.properties = properties;
return instance;
};
var a = namespace.child("a");
console.log(a.create());
a.get_private();
a.set_private("new_a_secret");
a.get_private();
var b = namespace.child("b");
console.log(b.create());
b.get_private();
a.get_private();
그렇다면'a'도'b'도 인스턴스화 할 수 없으므로 작동하지 않습니다. – Bergi
namespace.child는 클래스를 생성하는 팩토리 함수로 간주됩니다. init()은 초기화 함수의 이름을 짓는 더 좋은 방법입니다. .unique는 .properties를 기반으로 한 고유 한 것을 리턴한다는 것을 보여주기 위해 사용되었습니다 (문자열을 저장하는 것은 단지 예일뿐입니다). get_private, set_private 및 create (well init)는 각 클래스 인스턴스 (parent_method1 및 parent_method2 포함)간에 공유되어야한다고 가정합니다. 제안 된대로 상속 수준에 따라 팩터링 된 클래스에 대해 공유되도록하는 더 좋은 방법입니다. ? – humand
문제는 'create'(또는'init')가 클래스 * 인스턴스를 만드는 대신 클래스 자체를 실제로 초기화한다는 것입니다. "* 각 클래스 인스턴스간에 공유 *"라는 용어는 모호합니다. 클래스 객체 또는 인스턴스 객체를 의미합니까? – Bergi