一些JavaScript库是否使用原型系统的动态方面? [英] Does some JavaScript library use dynamic aspects of the prototype system?

查看:118
本文介绍了一些JavaScript库是否使用原型系统的动态方面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用原型创建的JavaScript对象维护与其原型的实时连接,因此更改原型也会影响从其创建的对象。

JavaScript object created with a prototype maintains "live" connection to its prototype, so that changing the prototype also affects the objects created from it.

语法如果新创建的对象只是复制了原型中的所有内容然后忘记了它,那么语言就会简单得多。当然,实际的底层实现可能更聪明。

The semantics of the language would be much simpler if newly created object just copied everything from the prototype and then forgot about it. The actual underlying implementation could be smarter, of course.

实时/动态连接的这个功能是否在一些众所周知的JavaScript库或程序中实际使用?

Is this feature of live/dynamic connection actually used in some well known JavaScript library or program?

编辑:我不是建议JS继承系统存在缺陷,我只是想了解上述功能的好处。

I'm not proposing JS inheritance system is flawed, I just want to understand the benefits of aforementioned feature.

推荐答案

当然。我认为这个功能最突出的用途是动态地将方法添加到jQuery原型(称为插件),它暴露为 jQuery.fn

Surely. I think the most prominent use of this feature is the dynamical adding of methods to the jQuery prototype (called "plugins"), which is exposed as jQuery.fn.

我无法理解为什么你认为如果新创建的对象只是复制了原型属性,那么语言的语义就会简单得多。为什么这会更简单?您可以使用 extend()函数创建具有该方法的对象,但这会触发原型继承的概念。

I can't understand why you think "the semantics of the language would be much simpler if newly created object just copied the prototype properties". Why would that be simpler? You can create objects with that method, using a extend() function, but that knocks the concept of prototypical inheritance on the head.

这是在Javascript中进行非静态继承的核心功能之一。修改已生成实例的原型对象非常有用:

It is one of the core features to have that non-static inheritance in Javascript. It is useful to modify the prototype object of already generated instances for:


  • 添加功能,如上所述。您只能在需要时加载整个插件。

  • 增强功能(有时)。这通常用于覆盖非标准行为。

  • 真正改变它们(很少)。这可以与配置对象一起使用,配置对象从具有默认设置的对象继承。然后你可以更改默认设置,它们将适用于所有实例(至少对那些没有覆盖相关属性的实例)。

这篇关于一些JavaScript库是否使用原型系统的动态方面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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