我应该使用原型吗? [英] Should I use prototype or not?

查看:134
本文介绍了我应该使用原型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Vector类,它基本上可以包含三个数值。但是,可以在这样的矢量上进行许多操作 - 例如得到幅度,增加或减去另一个向量等。

I'm creating a Vector class, which can basically hold three numerical values. However, a lot of operations can be done on such a vector - e.g. getting the magnitude, adding or subtracting another vector etc.

我想知道这些函数是否应编码为Vector类的原型函数,或者我应该定义它们在构造函数中。

I was wondering whether these functions should be coded as being a prototype function of the Vector class, or that I should define them in the constructor.

那么这两种方法中的哪一种更可取?

So which of these two methods is preferable?

function Vector3D(x, y, z) {
    this.x = x;
    this.y = y
    this.z = z;
}

Vector3D.prototype.magnitude = function() {
    return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
};

function Vector3D(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;

    this.magnitude = function() {
        return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
    };
}


推荐答案

这正是使用原型。我认为这样做有两个主要好处:

This is exactly the situation for using prototype. I see two main benefits for doing so:


  1. 多次创建函数。如果在构造函数中定义函数,则会为每个定义的函数创建一个新的匿名函数,每次调用构造函数时都会 。原型是静态对象,Vector3D的每个实例都只是引用原型函数。

  2. 原型是一个可以轻松操作的单个对象即可。这提供了很大的灵活性;不幸的是,我只能提供一些可以提供的例子:
  1. Functions are not created multiple times. If you define the functions inside the constructor, a new anonymous function is being created for each function you define, every time the constructor is called. Prototypes are static objects, and each instance of Vector3D will simply reference the prototype functions.
  2. The prototype is a single object that can be easily manipulated. This affords great flexibility; unfortunately I'm only able to provide a few examples of what this can offer:

  1. 如果你想创建一个子类,例如Vector3DSpecial,你可以简单地克隆 Vector3D.prototype 并将其分配给 Vector3DSpecial.prototype 。虽然您也可以使用 Vector3DSpecial.prototype = new Vector3D(); 使用构造函数执行此操作,但构造函数可能包含将在该简单原型赋值中执行的副作用,因此应该避免。使用原型,您甚至可以只选择原型中的特定函数复制到新类。

  2. 将方法添加到 Vector3D 只是向原型添加属性,并允许您的代码更容易拆分/组织成多个文件,或允许动态地在代码的其他部分添加方法。当然,你可以在构造函数和原型中组合添加方法,但这是不一致的,并且可能会导致更多的复杂性。

  1. If you wanted to create a child class, for example Vector3DSpecial, you can simply clone Vector3D.prototype and assign this to Vector3DSpecial.prototype. While you can also do this using constructors by Vector3DSpecial.prototype = new Vector3D();, constructors may contain side-effects which will get executed in that simple prototype assignment, and therefore should be avoided. With prototypes, you may even choose only particular functions in the prototype to be copied over to the new class.
  2. Adding methods to Vector3D is simply a matter of adding properties to the prototype, and allows your code to be more easily split / organised into multiple files, or to allow for adding methods in other parts of the code dynamically. Sure, you can do a combination of adding methods in the constructor and via the prototype, but that is inconsistent and is likely to lead to more complexity further down the track.


我什么时候使用原型?对于单例对象,例如与页面交互并可以将工作委托给其他对象的控制器。全局通知对象就是这样一个例子。在这里,扩展是不可能的,并且对象仅创建一次,使原型成为额外的(概念上的)复杂性。

When would I not use prototype? For singleton objects, for example a controller that interacts with a page and may delegate work off to other objects. A global "notification" object is one such example. Here, extending is unlikely, and the object is only created once, making the prototype an additional (conceptual) complexity.

这篇关于我应该使用原型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆