Javascript 原型继承和 OOP
我正在创建一个允许用户创建小部件的应用程序。有几种不同类型的小部件,我使用原型继承来定义它们。即
//the base widget that all widgets inherit from
var Widget = function(){}
Widget.prototype.someFunction = function(){}
//widget type A
var A = function(){}
A.prototype = new Widget();
//widget type B
var B = function(){}
B.prototype = new Widget();
我发现在基类上添加一个可以创建相同类型的新小部件实例的方法会很方便。即,
//the base widget
var Widget = function(){};
Widget.prototype.clone = function(){
switch(this.type){
case 'A':
return new A();
break;
case 'B':
return new B();
break;
default:
break;
}
};
这将允许我使用以下代码获得相同类型的新小部件:
var widgetTypeA = new A();
var cloneOfWidgetTypeA = widgetTypeA.clone();
我担心的是,基本小部件现在必须明确了解从它继承的每种类型的小部件。这是否违反了良好的 OOP 原则?
I'm creating an application that allows a user to create widgets. There are several different types of widgets, and I have defined them using protypal inheritance. i.e.
//the base widget that all widgets inherit from
var Widget = function(){}
Widget.prototype.someFunction = function(){}
//widget type A
var A = function(){}
A.prototype = new Widget();
//widget type B
var B = function(){}
B.prototype = new Widget();
I have discovered that it will be convenient to add a method on the base class that can create a new widget instance of the same type. i.e.
//the base widget
var Widget = function(){};
Widget.prototype.clone = function(){
switch(this.type){
case 'A':
return new A();
break;
case 'B':
return new B();
break;
default:
break;
}
};
Which would allow me to get a new widget of the same type using the following code:
var widgetTypeA = new A();
var cloneOfWidgetTypeA = widgetTypeA.clone();
My concern is that the base widget now has to be explicitly aware of each of the types of widgets that inherit from it. Does this violate any principles of good OOP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然,假设所有子类都被声明为全局变量。
但老实说,我会在 Widget 命名空间中取出这些子类,并通过它访问它们,而不是将所有内容都全局化。
Assuming that all your subclasses are declared as globals of course.
But honestly I would out those sub classes in the Widget namespace, and access them through it rather than making everything global.
鉴于您的构造函数是全局的,您可以执行以下操作:
假设每个实例都有一个 type 属性,其值是构造函数的名称。或者您可以修复构造函数原型的构造函数属性并执行以下操作:
但是,这假设您没有任何要传递的参数。如果您确实需要传递参数,那么您可能必须知道您正在创建哪种类型,以便无论如何都可以调用正确的构造函数。
Given that your constructors are globals, you could do something like:
Provided each instance has a type property whose value is the name of the constructor. Or you could fix the constructor property of constructor's prototype and do:
However, that assumes that you don't have any parameters to pass. If you do have parameters to pass, then you likely have to know which type you are making and so can call the correct constructor anyway.
如果支持 ECMA5:
Object.create(Object.getPrototypeOf(this));
如果不支持 ECMA5:
this.__proto__
示例:
If ECMA5 is supported:
Object.create(Object.getPrototypeOf(this));
If ECMA5 is not supported:
this.__proto__
Example:
您需要的信息已在
constructor
属性中提供。但是,覆盖prototype
会丢失它,因为我最近 在这里解释。使用我自己的 ECMAScript 类实现 版本 3 或 版本5,您的示例将如下所示:
The information you need is already available in the
constructor
property. However, overwritingprototype
will lose it as I recently explained here.Using my own class implementation for ECMAScript version 3 or version 5, your example would look like this: