“类"内部或外部的原型函数有什么区别?定义 [英] What is the difference between a prototype function inside or outside the "class" definition

查看:44
本文介绍了“类"内部或外部的原型函数有什么区别?定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
在 Javascript 中使用原型"还是这个"?
在构造函数中定义原型方法

这两个定义有什么区别吗?

is there any difference between these two definitions?

function Task(description, value) {

    this.id = 0;
    this.subId = 0;
    this.parent = null;
    this.children = new Array();

    this.description = description;
    this.value = value;

    Task.prototype.getNextId = function () {
       return this.subId++;
    },

    Task.prototype.addTask = function (task) {
        task.id = this.getNextId();
        this.children[task.id] = task;
        task.parent = this;
    },

    Task.prototype.remove = function () {
        this.parent.children.remove(this.id);
    }

}

所有原型方法都在Task定义中,还是?

All prototype methods are inside the Task definition, or?

function Task(description, value) {

    this.id = 0;
    this.subId = 0;
    this.parent = null;
    this.children = new Array();

    this.description = description;
    this.value = value;

}

Task.prototype.getNextId = function () {
   return this.subId++;
},

Task.prototype.addTask = function (task) {
    task.id = this.getNextId();
    this.children[task.id] = task;
    task.parent = this;
},

Task.prototype.remove = function () {
    this.parent.children.remove(this.id);
}

我不确定是否存在差异.从 OOP 的角度来看,内部定义看起来更好.

I'm not sure wether there is a difference or not. From the OOP view the inside definition looks better.

谢谢!

推荐答案

构造函数的 prototype 是一个对象,在从构造函数创建的实例之间共享.

The prototype of a constructor is an object that is shared among instances created from the constructor.

如果您在构造函数中更新 prototype 对象的属性,则共享该原型对象的所有实例都将引用最新更新.

If you update the properties of the prototype object inside the constructor, all the instances that share that prototype object will reference the latest update.

在您的代码中,您不会注意到有什么不同,但它仍然是错误的.无需使用新的相同版本继续覆盖原型函数.

In your code you won't notice a difference, but it's still wrong. There's no need to keep overwriting the prototyped functions with new identical versions.

如果您的代码发生更改,以便添加到 prototype 的函数在构造函数中引用局部变量,那么所有实例都将最终使用在最新调用中引用变量的原型函数.这几乎不是您想要的.

If your code changes so that the functions added to the prototype reference local variables in the constructor, then all instances will end up using the prototyped function that references the variables in the newest invocation. This is almost never what you'd want.

如果您的代码发生更改,从而覆盖了构造函数的整个原型对象,那么创建的每个实例都将引用不同的原型对象,并且您将无法通过添加新方法来更新所有实例的原型Task.prototype,因为它只会更新最近实例的原型对象.

If your code changes so that the entire prototype object of the constructor is overwritten, then each instance created will be referencing a different prototype object, and you won't be able to update the prototype of all instances by adding new methods to Task.prototype, because it will just be updating the prototype object of the most recent instance.

这篇关于“类"内部或外部的原型函数有什么区别?定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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