为什么/如何在 JavaScript 中使用对象?
我了解如何实例化对象并调用它们,但我就是找不到在脚本中使用它们的理由。我可以这样做
var obj = {
hi: function() {
return "Hello";
}
};
,但为什么我不能以同样的方式这样做:
function hi() {
return "Hello";
}
我也从来不明白为什么我应该使用原型设计。我在 JavaScript 中所做的大部分事情无需对象也能做得很好。但我想使用对象。对象的用途是什么以及我应该使用它们的原因是什么?
I understand how to instantiate objects and call them, but I just cannot find a reason to use them in my script. I could do
var obj = {
hi: function() {
return "Hello";
}
};
but why can't I just do it the same way like:
function hi() {
return "Hello";
}
I've never understood the reasons why I should use prototyping either. Most of the things I do in JavaScript I can do well without objects. But I want to use objects. What are objects for and what are the reasons why I should use them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
例如,对象可用于将属于在一起的值制成单个单位。示例:
与结构较少的相比:
Objects are useful for example for making a single unit out of values that belong together. Example:
Compared to the less structured:
对象很有用,因为
如果您不需要非全局状态,不需要查找表,并且您的问题很小或更容易在功能上分解,那么不要使用对象。
Objects are useful because
If you don't need non-global state, have no need of lookup tables, and your problem is small or more easily decomposed functionally then don't use objects.
如果没有您所指的对象,您将到处都有松散的功能。这通常会导致代码非常难以维护。至少,对象使您能够将函数集中在一起以模拟命名空间——而且这是最低限度的。
Without what you are referring to as objects, you are going to have loose functions all over the place. This very often will result in code that is very difficult to maintain. At a bare minimum objects give you the ability to lump functions together in order to simulate namespaces-- and that's at a bare minimum.
在您的简单示例中,编写半“类”/对象来保存该方法确实没有意义。但是,当您的代码增长时,您会获得越来越多的函数和方法,您并不真的希望将它们全部放在一个大(全局)命名空间中。这是不可能维护的,没有人会理解该代码,包括你在内。
这是将方法包装在对象/“类”中的第一个充分理由。另一个很好的理由是可重用性。如果您正在编写能够继承其方法的对象,则可以重新创建另一个对象并从那里抽象它。最简单的概念,但如果您将应用程序中的“事物”描述为模块/对象,则需要使用它。
In your simple example, it makes indeed no sense to write a semi "class" / object to hold that method. But when your code grows, you're getting more and more functions and methods, you don't really want to have them all in one big (global) namespace. That is just so impossible to maintenain, no one will understand that code including you at some later point.
That is the first good reason to wrap methods together in an object/"class". Another good reason is re-usabilty. If you're writting objects which are able to inherit their methods, you can re-create another object and abstract it from there on. Most simple concept, but you want to use it if you're describing "things" in your application as module/object.
它试图模拟 OOP 范式,仅此而已。有多种方法可以做到这一点。但问问自己,“hi”是否属于“obj”,或者它可以是一个独立的函数吗?这完全取决于函数与对象的相关程度。该函数是否需要访问对象私有变量等?
It tries to simulate the OOP paradigm that's all. There are various ways of doing this. But ask yourself, doe 'hi' belong in 'obj' or can it be a standalone function? Its all about how closely related is the function to the object. Does the function needs to access the objects private variables and such?
这不是一个“Javascript 中的对象”,而是一个“一般的对象”问题。
我想说的是最相关的 Javascript。Javascript 与对象相关的唯一特定之处是它们在命名空间时的巧妙使用。例如,大多数 Javascript 库都包含所有内容他们的东西在单个对象中以避免名称空间冲突:
至于为什么 OOP 的其他问题,我认为 OOP 擅长两个基本的事情(通用的,愚蠢的,示例如下):
动态调度 - 摆脱“ifs” ”并将责任放在其所属的位置
当您查看包含大量“开关”的代码时:
将每种不同类型的行为捆绑在不同类型的对象中并使用动态调度可能是一个好主意:
现在,如果您将来想出另一种动物,您无需查看代码来添加它 - 您所需要做的就是使用适当的 print_speack 和 print_food_type 方法创建一个新类其余代码不会注意到任何事情。
继承+重用方法
在普通的 OO 语言中,每个对象都存储(并消耗内存)其实例变量,而所有方法 + 静态变量都由类存储在单个位置。 Javascript 没有类,但它有原型,并且它们最终提供相同的基本功能。
This is less of a "objects in Javascript" and more of an objects in general" question.
I'd say the most relevant Javascript The only thing Javascript specific about objects is their neat use when namespacing. For example, most Javascript libraries pack all their stuff in a single object to avoid namespace collision:
As for the other questions of why OOP, there are 2 basic things I think OOP excels at (generic, dumb, examples follow):
Dynamic dispatching - get rid of "ifs" and put responsabilitty where it belongs
When you look at a code that has tons of "switchs":
It might be a good idea to bundle each different kind of behaviour in a different kind of object and use dynamic dispatching instead:
Now, if you come up with another kind of animal in the future, you don't need to go looking around your code to add it - all you need to do is create a new class with the appropriate print_speack and print_food_type methods and the rest of the code won't notice a thing.
Inheritance + reusing methods
In a normal OO language each object stores (and spends memory) for its instance variables, while all methods + static variables are stored in a single place by the class. Javascript doesn't have classes, but it has prototypes and they serve the same basic function in the end.
关于“为什么”部分的一些一般想法:
对象的存在是为了通信。这是参与项目的每个人的通用词汇。人们习惯于操作事物(对象)及其操作(消息或方法),而不仅仅是使用互不相关的操作(功能)。
Some generic thoughts concerning "Why" part:
Objects exist to communicate. It is a common vocabulary for everyone involved in a project. People are used to operate with things (objects) and their operations (messages or methods), rather than just with disconnected actions (functions).