我什么时候想使用“class”? JavaScript 中的(静态)方法或属性?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(编辑:在其原始形式中,问题询问了将属性附加到类还是将它们附加到原型,这就是我要回答的内容。)
这实际上更多的是一个约定问题 。 。如果您编写
(其中
Human::specie
是Human.prototype.specie
的 CoffeeScript 简写),然后声明jane = new Human
,则jane.specie
将是“Homo sapiens”
(除非您专门将jane.specie
设置为其他内容)。在这种情况下,这听起来是可取的。但在其他情况下,在大量原型之间共享属性会使您的代码更难以理解。假设您有一个带有
config
对象的Logger
类。如果您将该对象附加到原型,那么您可以编写如下代码:这会将所有
Logger
实例的目标更改为'./foo'< /code>,因为只有一个
config
对象。如果您希望 config 应用于所有 Logger 实例,那么您应该将其附加到类本身,从而消除上面代码中的歧义:(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
(where
Human::specie
is CoffeeScript shorthand forHuman.prototype.specie
) then declarejane = new Human
, thenjane.specie
will be"Homo sapiens"
(unless you specifically setjane.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 aconfig
object. If you attached that object to the prototype, then you could write code like this:This would change the destination of all
Logger
instances to'./foo'
, because there's only oneconfig
object. If you wantconfig
to apply to allLogger
instances, then you should attach it to the class proper, removing the ambiguity from the code above:在游戏中假设你有一个名为世界的对象。然而,游戏中永远只有一个世界。从理论上讲,这就是您这样做的原因。
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.
简而言之,所发布问题的答案是名称间距。有些值可能有意义在程序中共享,并且在语义上与某些类有关。这些函数和值可以只放入一些变量中,但将它们附加到构造函数是实用的,以便为它们命名空间。
最好的例子是 JavaScript
Math
类(对于纯粹主义者来说,我知道它实际上不是一个类,它是一个对象):因此方法和值(保存在常量中)始终相同,并且它们不同取决于它们被调用的实例,将它们放在实例上是没有意义的。
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):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.