jQuery的JavaScript命名空间 [英] JavaScript Namespace with jQuery

查看:63
本文介绍了jQuery的JavaScript命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据jQuery管理自定义JavaScript库的命名空间?

How do you manage namespace for a custom JavaScript library depends on jQuery?

您是否创建自己的名称空间(例如foo)并在其中添加对象?例如foo.myClass, foo.myFunction

Do you create your own namespace, say foo and add your objects there? e.g. foo.myClass, foo.myFunction

还是将对象添加到jQuery的名称空间?例如jQuery.myClass, jQuery.myFunction

Or do you add your objects to jQuery's namespace? e.g. jQuery.myClass, jQuery.myFunction

哪种是较常见的做法,为什么?

Which is the more common practice and why?

推荐答案

这取决于库的功能.

  • 如果要扩展jQuery对象实例的功能,请使用jQuery.fn,如他的答案中.

如果要创建的实用程序应被视为对window.jQuery中提供的实用程序的补充,那么我认为使用该名称空间不会有问题(只要您谨慎命名) ).

If you're creating utilities that are meant to be seen as additions to those provided in window.jQuery, then I don't see a problem with using that namespace (as long as you're careful with naming).

如果它确实是自己的独立库,不是 ,而是要依赖jQuery的功能,那么肯定使用您自己的命名空间. p>

If it is really its own separate library that is not meant to be seen as an extension of jQuery, yet relies on functionality from jQuery, then definitely use your own namespace.

这篇关于jQuery的JavaScript命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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