class 实例的私有成员是否有更优雅的方法?
Is there a more elegant approach for private members of class instances?
我想为 class- 以及 class-实例创建设定一个设计标准。结合来自多个网站(例如来自 Whosebug)的大量信息,终于有了一种相对最大灵活性的方法。我的目标正是使代码结构的行为类似于 Java 等更明确的 classes 等。
这是我目前所拥有的工作代码片段(包括解释):
var MyClass = function (prop1)
{
var _class = MyClass;
var _proto = _class.prototype;
// public member of constructed class-instance
this.prop1 = prop1;
// private static property as well as private member of class-instances
// (see getter below!)
var prop2 = this.prop1 * 10;
// need to use this. instead of _proto because the property prop2 of
// the class itself would be returned otherwise
this.getProp2 = function ()
{
return prop2;
}
// 1 function for all instances of the class
// reached by a fallback to the prototype
_proto.protoAlert = function ()
{
// must call this.getProp2() instead of using prop2 directly
// because it would use the private static member of the class
// itself instead the one of the class-instance
alert(this.prop1 + " " + this.getProp2());
}
};
var c1 = new MyClass(1);
c1.protoAlert();
var c2 = new MyClass(2);
c2.protoAlert();
c1.protoAlert();
到目前为止效果很好。但是,要避免引发错误和未发现的脚本不当行为,需要克服一些障碍。私有 属性 prop2
存在于 class 和 class 实例中。这可能是一种无意识的双重身份。此外,class-实例的私有 属性 只能通过 setter- 和 getter- 函数正确访问。这不是最糟糕的事情,因为它强制使用一种通用的方式来访问私有变量。缺点是:Setter 和 getter 必须用 this.
调用才能实际引用 class 实例的 prop2
然后返回它。至于 class 继承——我还没有用我目前的标准研究这个话题。希望它也会成功。
是否有更优雅的解决方案或至少一种不太容易出错的解决方案?
提前致谢!
JavaScript 并没有真正提供私有属性的实用模式。只要您在构造函数中定义了所有方法,您使用的模式就可以正常工作。您应该记住,这意味着每次创建 class 时,都会创建所有方法。
如果你仔细想想,私有变量在程序中没有任何作用,它们是为程序员服务的要牢记,他应该和他不应该改变的。因此,您可以简单地使用一些命名模式。我在其他人的代码中看到了很多:
function MyClass() {
// Private property
this._helloWord = "Hello word.";
}
// From outside, accessed as `helloWord`, without underscore
Object.defineProperty(MyClass.prototype, "helloWord", {
get: function() {console.log("helloWord getter");return this._helloWord;},
set: function(value) {console.log("helloWord setter");return this._helloWord = value;},
};
MyClass.prototype.alertProp = function() {
alert(this._helloWord);
}
// Accessing from the outside:
var instance = new MyClass();
alert(instance.helloWord); // will activate the getter function
大多数人会立即明白 _underscored
变量有一些特别之处。你也可以这样使变量常量:
Object.defineProperty(MyClass.prototype, "helloWord", {
value: "Hello world",
writable: false // <----
};
了解有关 Object.defineProperty
的更多信息。您还应该了解 Javascript 的结构与 OOP 语言中的结构略有不同。如果你试图将其他语言的模式强加到它上面,将会导致性能和结构问题。
我想为 class- 以及 class-实例创建设定一个设计标准。结合来自多个网站(例如来自 Whosebug)的大量信息,终于有了一种相对最大灵活性的方法。我的目标正是使代码结构的行为类似于 Java 等更明确的 classes 等。
这是我目前所拥有的工作代码片段(包括解释):
var MyClass = function (prop1)
{
var _class = MyClass;
var _proto = _class.prototype;
// public member of constructed class-instance
this.prop1 = prop1;
// private static property as well as private member of class-instances
// (see getter below!)
var prop2 = this.prop1 * 10;
// need to use this. instead of _proto because the property prop2 of
// the class itself would be returned otherwise
this.getProp2 = function ()
{
return prop2;
}
// 1 function for all instances of the class
// reached by a fallback to the prototype
_proto.protoAlert = function ()
{
// must call this.getProp2() instead of using prop2 directly
// because it would use the private static member of the class
// itself instead the one of the class-instance
alert(this.prop1 + " " + this.getProp2());
}
};
var c1 = new MyClass(1);
c1.protoAlert();
var c2 = new MyClass(2);
c2.protoAlert();
c1.protoAlert();
到目前为止效果很好。但是,要避免引发错误和未发现的脚本不当行为,需要克服一些障碍。私有 属性 prop2
存在于 class 和 class 实例中。这可能是一种无意识的双重身份。此外,class-实例的私有 属性 只能通过 setter- 和 getter- 函数正确访问。这不是最糟糕的事情,因为它强制使用一种通用的方式来访问私有变量。缺点是:Setter 和 getter 必须用 this.
调用才能实际引用 class 实例的 prop2
然后返回它。至于 class 继承——我还没有用我目前的标准研究这个话题。希望它也会成功。
是否有更优雅的解决方案或至少一种不太容易出错的解决方案?
提前致谢!
JavaScript 并没有真正提供私有属性的实用模式。只要您在构造函数中定义了所有方法,您使用的模式就可以正常工作。您应该记住,这意味着每次创建 class 时,都会创建所有方法。
如果你仔细想想,私有变量在程序中没有任何作用,它们是为程序员服务的要牢记,他应该和他不应该改变的。因此,您可以简单地使用一些命名模式。我在其他人的代码中看到了很多:
function MyClass() {
// Private property
this._helloWord = "Hello word.";
}
// From outside, accessed as `helloWord`, without underscore
Object.defineProperty(MyClass.prototype, "helloWord", {
get: function() {console.log("helloWord getter");return this._helloWord;},
set: function(value) {console.log("helloWord setter");return this._helloWord = value;},
};
MyClass.prototype.alertProp = function() {
alert(this._helloWord);
}
// Accessing from the outside:
var instance = new MyClass();
alert(instance.helloWord); // will activate the getter function
大多数人会立即明白 _underscored
变量有一些特别之处。你也可以这样使变量常量:
Object.defineProperty(MyClass.prototype, "helloWord", {
value: "Hello world",
writable: false // <----
};
了解有关 Object.defineProperty
的更多信息。您还应该了解 Javascript 的结构与 OOP 语言中的结构略有不同。如果你试图将其他语言的模式强加到它上面,将会导致性能和结构问题。