为什么在原型中定义属性被认为是反模式 [英] Why defining properties in the prototype is considered an antipattern

查看:134
本文介绍了为什么在原型中定义属性被认为是反模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到这种模式来定义javascript对象

I often see this pattern to define javascript objects

function Person(name) {
    this.name = name;
}
Person.prototype.describe = function () {
    return "Person called "+this.name;
};

这篇文章它说直接向原型objct添加属性被认为是反模式。

And in this article it says that adding properties directly to the prototype objct is considered an anti-pattern.

来自经典基于类的语言,除了方法之外必须定义属性听起来不太正确,不过在javascript中,一个方法应该只是一个具有函数值的属性(我在这里吗?)

Coming from "classical class based" languages, having to define the properties apart from methods doesn't sound quite right, moreoever in javascript, where a method should be just a property with a function value (am I right here?)

我想知道是否有人可以解释这一点,或者甚至建议更好的方法来处理这些情况

I wanted to know if anybody can explain this, or even suggest a better way to handle these situations

推荐答案

在通常的面向对象语言中,你有一个描述成员,方法和构造函数的类的定义。

In usual object-oriented languages, you have a definition of the class describing members, methods and the constructor.

在JS中,类的定义(它实际上并不像其他语言那样......有时使用术语伪类)是构造函数本身。如果您的对象由 name 进行参数化,则编写

In JS, the definition of the "class" (it is not really class like in other languages... sometimes the term pseudoclass is used) is the constructor itself. If your object is parametrised by name, it makes sense to write

function Person(name) {
    this.name = name;
}

即。必须在构造函数中设置属性 name

i.e. the property name must be set in the constructor.

当然,你可以写

function Person(name) {
    this.name = name;
    this.describe = function() { ... };
}

它会按预期工作。

但是,在这种情况下,每次调用构造函数时都会创建一个单独的方法实例。

However, in this case you are creating a separate instance of the method with every call of the constructor.

另一方面,这里:

Person.prototype.describe = function () {
    return "Person called "+this.name;
};

您只需定义一次方法。 Person 的所有实例都将收到一个指针(称为 __ proto __ ,程序员无法在大多数浏览器中访问)到 Person.prototype的。所以如果你打电话

you only define the method once. All instances of Person will receive a pointer (called __proto__ and not accessible by programmer in most browsers) to Person.prototype. So if you call

var myPerson = new Person();
myPerson.describe();

它会起作用,因为JS直接在对象中查找对象成员,然后在其原型等中查找所有对象成员到达 Object.prototype 的方式。

it will work, because JS looks object members directly in the object, then in its prototype etc. all the way up to Object.prototype.

关键是在第二种情况下,只有一个实例功能将存在。你可能会认为这是一个更好的设计。即使你不这样做,也只需要更少的内存。

The point is that in the second case, only one instance of the function will exist. Which you will probably agree that is a better design. And even if you don't, it simply takes less memory.

这篇关于为什么在原型中定义属性被认为是反模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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