在 JavaScript 中使用继承模式

发布于 2024-09-18 18:59:27 字数 243 浏览 9 评论 0原文

根据我的经验,JavaScript 是这样做的:

  • 操作 DOM 或其他主机对象
  • 添加事件处理程序
  • 执行 Ajax

自从我开始深入研究原型继承以来,我想知道它在实践中是如何实际使用的。有哪些用例?

这里有人积极使用继承模式吗?做什么的?

(我知道我的问题有很多答案 - 我只是想听听其中的一些来感受一下在 JavaScript 中使用继承的感觉)

From my experiance, JavaScript does this:

  • manipulating the DOM or other host objects
  • adding event handlers
  • doing Ajax

Since I started digging into prototypal inheritance, I would like to know how it is actually used in practice. What are the use cases?

Is anyone here actively using inheritance patterns? What for?

(I understand that there are lots of answers to my question - I just would like to hear some of them to get the feel of using inheritance in JavaScript)

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

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

发布评论

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

评论(5

千笙结 2024-09-25 18:59:28

Douglas Crockford 有一些关于各种继承的有趣文章:

http://javascript.crockford.com/inheritance.html< /a>

http://javascript.crockford.com/prototypal.html

http://javascript.crockford.com/private.html

第一个是旧的,你可能不想要这样做任何事情,但它显示了 JavaScript 的灵活性。第二个是他如何使用原型。如果您要在 JavaScript 中大量使用对象,最后一个会很有趣,并且它有一些关于继承的内容。

阅读它们(它们很短)可能不会给你所有你想要的答案,但我怀疑它会让你以稍微不同的方式思考和看待 JavaScript。也就是说,克罗克福德总是对他的写作非常有信心,但我认为你不应该把他的话当作法律。

Douglas Crockford has some interesting articles regarding various inheritance:

http://javascript.crockford.com/inheritance.html

http://javascript.crockford.com/prototypal.html

http://javascript.crockford.com/private.html

The first one is old, and you might not want to do anything that way, but it shows how flexible JavaScript can be. The second is just how he uses prototypes. The last one is just interesting if you are going to use objects much in JavaScript, and it has a bit about inheritance.

Reading them (they're short) may not give you all of the answers you want, but I suspect it will make you think about and see JavaScript in a slightly different way. That said, Crockford is always very confident in his writing, but I don't think you should take his word as law.

怎言笑 2024-09-25 18:59:28

您应该查看任何大型库的代码,例如 jQuery - 它在这些库中使用得非常广泛。

You should have a look at the code for any of the big libraries, like jQuery - it's used quite extensively within these.

生生漫 2024-09-25 18:59:28

我所知道的 JavaScript 中继承的最好例子可能是 Ext 库。与 jQuery 不同,jQuery 主要是一个可以扩展自身的大对象,Ext 展示了一种在应用面向对象范例的程序中发现的更典型的继承方法。

Ext 是一个库,由许多可以以多种方式使用的可配置对象组成。大多数对象都被概括为抽象类。看看吧。

该工具已经存在很长时间了,但 ajax 和更厚的客户端 Web 应用程序的趋势意味着它变得越来越普遍,尽管它通常打包在您正在使用的库中。

我以前尝试过原型继承,并且在我的全职工作期间在一些工作中很少使用它。了解它很有用,并且了解它也很好,但我日常使用它的用例并不多。

Probably the best example of inheritance used in JavaScript I know of is the Ext library. Rather than jQuery which is mostly a big object that gets extended itself, Ext shows a more typical approach to inheritance found in programs applying object oriented paradigms.

Ext is a library that comprises of many configurable objects that can be used in various ways. Most of the objects are generalized into abstract classes. Give it a look.

The facility has been there for a long time but trends towards ajax and thicker client web applications means it is becoming more common although it is normally packaged up within the libraries you are using.

I've played around with prototypical inheritance before and have used it sparingly in some work during my full time job. It is useful to know and good to get your head around it, but there aren't really that many use cases I've had for it day to day.

放血 2024-09-25 18:59:27

我使用 jQuery(以及之前的 JavaScript)的经验是,原型继承并不像我预期的那么有用。它有用途,但对于语言来说并不重要。

在 Javascript 中,如果你想使用方法 foo 返回一个对象:

return { 
  foo: function() { 
    alert('You called foo!'); 
  }
};

并且调用者可以将此类对象视为多态 - 也就是说,他们可以调用 foo 而不必担心“类型” “它是一个对象。没有必要继承。

在此背景下,原型只是一种优化。它们允许您创建大量对象,而无需在每个实例中复制大量函数属性。这就是 jQuery 在内部使用它的原因。一个 jQuery 对象有几十个函数,将它们复制到每个实例中可能会产生很大的开销。

但从 jQuery 用户的角度来看,原型并不是特别重要。可以重写它以不使用它们,并且它仍然可以工作(但可能会使用更多内存)。

My experience working with jQuery (and JavaScript before it) is that prototypical inheritance isn't as useful as I was expecting. It has uses, but it's not fundamentally important to the language.

In Javascript if you want to return an object with a method foo:

return { 
  foo: function() { 
    alert('You called foo!'); 
  }
};

And callers can treat such objects as polymorphic - that is, they can call foo without worrying about what "type" of object it is. There is no need for inheritance.

Against this background, prototypes are merely an optimisation. They allow you to create a large number of objects without having to replicate a big set of function properties in each instance. This is why jQuery uses it internally. A jQuery object has dozens of functions, and it might be a major overhead to copy them into every instance.

But from the perspective of a user of jQuery, prototypes aren't particularly important. It could be rewritten to not use them and it would still work (but might use a lot more memory).

铁轨上的流浪者 2024-09-25 18:59:27

随着越来越多的代码是专门为运行 iOS、Android 等的移动设备编写的,并且随着应用程序范式的成熟,人们正在转向将越来越多的复杂性和逻辑放入设备上运行的 JavaScript 库中。

JavaScript 的对象/继承模型为开发人员提供了一个非常灵活的工具包,用于开发动态和复杂的应用程序。

JavaScript 继承模型允许您:

  • 在代码中的任何位置扩展原型(对象定义),即使在实例之后
    对象已创建。
  • 扩展原型的实例,使其与其他实例分开。
  • 使用新功能扩展所有内置对象。

这类似于 ruby​​ 的类和 mixin 功能,并开辟了大量新的设计和继承模式,其中只有少数被发明或记录。

jquery 是成熟的 javascript 库的最好例子。 链接文本
原型和脚本是其他的。

As more and more code is written specifically for mobile devices running iOS, Android, et.al, and as the app-paradigm matures, there is a shift towards putting more and more complexity and logic into javascript libraries running on the devices.

The Object/inheritance model of javascript give the developer a very flexible toolkit for developing dynamic and complex applications.

The JavaScript inheritance model lets you:

  • extend a prototype (object definition) any where in your code, even after instances of
    the object has been created.
  • Extend an instance of a prototyp separate from other instances.
  • Extend all builtin objects with new functionality.

This is similar to the class and mixin features of ruby, and opens up a ton of new design and inheritance patterns, of only a few has been invented or documented.

jquery is the best example of a mature javascript library. link text
prototype and scriptaculus are others.

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