我什么时候想使用“class”? JavaScript 中的(静态)方法或属性?

发布于 2024-11-17 18:58:20 字数 367 浏览 2 评论 0原文

在 JavaScript 中,为什么要将属性直接附加到构造函数?

var Human = function() {};
Human.specie = "Homo Sapience";

在查看了 CoffeeScript 的 __extend 辅助函数后,我得到了这个问题,该函数包含以下行:

for ( var key in parent ) { 
  if ( __hasProp.call( parent, key ) ) child[key] = parent[key]; 
} 

它将属性/方法直接从构造函数对象复制到子类对象。但为什么有人会这么做呢?

谢谢!

In JavaScript, why would one want to attach properties directly to the constructor?

var Human = function() {};
Human.specie = "Homo Sapience";

I've got this question after looking at CoffeeScript‘s __extend helper function, which contains, among the lines:

for ( var key in parent ) { 
  if ( __hasProp.call( parent, key ) ) child[key] = parent[key]; 
} 

which copies properties / methods to the subclassed object directly from the constructor object. But why would anybody do that?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就像说晚安 2024-11-24 18:58:20

编辑:在其原始形式中,问题询问了将属性附加到类还是将它们附加到原型,这就是我要回答的内容。)

这实际上更多的是一个约定问题 。 。如果您编写

Human::specie = "Homo sapiens"

(其中 Human::specieHuman.prototype.specie 的 CoffeeScript 简写),然后声明 jane = new Human,则 jane.specie 将是 “Homo sapiens”(除非您专门将 jane.specie 设置为其他内容)。在这种情况下,这听起来是可取的。

但在其他情况下,在大量原型之间共享属性会使您的代码更难以理解。假设您有一个带有 config 对象的 Logger 类。如果您将该对象附加到原型,那么您可以编写如下代码:

log = new Logger
log.config.destination = './foo'

这会将所有 Logger 实例的目标更改为 './foo'< /code>,因为只有一个 config 对象。如果您希望 config 应用于所有 Logger 实例,那么您应该将其附加到类本身,从而消除上面代码中的歧义:

log = new Logger
Logger.config.destination = './foo'

(Edit: In its original form, the question asked about attaching properties to classes vs. attaching them to prototypes, so that's what I'm responding to.)

It's really more a matter of convention than anything else. If you write

Human::specie = "Homo sapiens"

(where Human::specie is CoffeeScript shorthand for Human.prototype.specie) then declare jane = new Human, then jane.specie will be "Homo sapiens" (unless you specifically set jane.specie to something else). In this case, that sounds desirable.

But in other cases, having a property shared across a large number of prototypes makes your code harder to understand. Let's say that you have a Logger class with a config object. If you attached that object to the prototype, then you could write code like this:

log = new Logger
log.config.destination = './foo'

This would change the destination of all Logger instances to './foo', because there's only one config object. If you want config to apply to all Logger instances, then you should attach it to the class proper, removing the ambiguity from the code above:

log = new Logger
Logger.config.destination = './foo'
放飞的风筝 2024-11-24 18:58:20

在游戏中假设你有一个名为世界的对象。然而,游戏中永远只有一个世界。从理论上讲,这就是您这样做的原因。

In a game say you have an object called world. However there will only ever be one world in the game. This is theoretically the reason you would do this.

江湖正好 2024-11-24 18:58:20

简而言之,所发布问题的答案是名称间距。有些值可能有意义在程序中共享,并且在语义上与某些类有关。这些函数和值可以只放入一些变量中,但将它们附加到构造函数是实用的,以便为它们命名空间。

最好的例子是 JavaScript Math 类(对于纯粹主义者来说,我知道它实际上不是一个类,它是一个对象):

// There are constants
Math.E
Math.PI
Math.SQRT2
// And there are also methods
Math.ceil
Math.cos
Math.sin

因此方法和值(保存在常量中)始终相同,并且它们不同取决于它们被调用的实例,将它们放在实例上是没有意义的。

In short the answer to the question posted is name spacing. There are certain values that may have sense to be shared across your program and that semantically have to do with certain class. These functions and values could be just put in some variables, but attaching them to constructor function is practical in order to namespace them.

The best example is JavaScript Math class (for purists, I know it's not really a class, it's an object):

// There are constants
Math.E
Math.PI
Math.SQRT2
// And there are also methods
Math.ceil
Math.cos
Math.sin

So the methods and values (saved in constants) are always the same and they don't depend on instance that they are being called on and it makes no sense to have them on instances.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文