为什么人们将自己的自定义/用户函数添加到jQuery对象? [英] Why do people add their own custom/user functions to the jQuery object?

查看:57
本文介绍了为什么人们将自己的自定义/用户函数添加到jQuery对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过人们将自己的自定义/用户功能添加到 jQuery 对象( $ )。例如:

I've seen people add their own custom/user functions to the jQuery object ($). For instance:

$.myUserFunc = function() { /* regular JS code */}

你为什么要这样做?而不是 $ .myUserFunc ,为什么不简单地创建你的函数 myUserFunc 或(如果你想避免污染全局) scope)把它放在另一个自定义/用户对象上,比如 myObj.myUserFunc

Why would you do this? Instead of $.myUserFunc, why not create your function as simply myUserFunc or (if you want to avoid polluting the global scope) put it on another custom/user object like myObj.myUserFunc?

推荐答案

您要问的构造:

$.myUserFunc = function() {}

创建静态函数(可用)全局)在jQuery名称空间对象上。这与jQuery插件方法不同。

creates a static function (available globally) on the jQuery namespace object. This is different from a jQuery plug-in method.

为什么有人可能会这样做有几个原因 - 有些可能比其他人更合理。

There are several reasons why one might do this - some are perhaps better justified than others.


  1. 您可以创建一个与jQuery直接相关的jQuery实用程序函数(仅在使用jQuery时有用),但它与插件不同在方法中。

  1. You may create a jQuery utility function that is directly related to jQuery (only useful when using jQuery), yet is at a different level than a plug-in method.

您有一个全局函数,您不希望将其置于全局命名空间中(出于所有典型原因,许多顶级全局名称都是你正好使用jQuery,所以你选择将你的全局函数放在jQuery namspace对象上。

You have a global function that you don't want to put in the global namespace (for all the typical reasons that lots of top-level global name are avoided) and you happen to be using jQuery so you choose to put your global function on the jQuery namspace object.

在我看来,第一个是有道理的。如果它是一个特定于jQuery的函数,但它是一个全局函数而不是jQuery对象的方法(就像插件一样),那么把它放在主jQuery上就有了一些意义。当有一个适当的命名空间并且你的函数与那个命名空间对象相关时,真的没有理由再实现另一个顶级命名空间。

In my opinion, the first makes some sense. If it's a jQuery-specific function, yet it's a global function and not a method of a jQuery object (like plug-ins are), then putting it on the main jQuery makes some sense. There's really no reason to implement yet another top level namespace when there's an appropriate one already there and your function is related to that namespace object.

在我看来,第二个原因从架构的角度来看并没有那么多意义。如果此函数不是特定于jQuery的函数(例如,不使用或操作jQuery对象),那么它实际上不属于jQuery对象。它是一个通用函数,应该以更通用的方式定义,或者作为单例全局函数定义,或者,如果你有多种类型的相关函数,就把它放在它自己的命名空间对象上。

In my opinion, the second reason doesn't make as much sense from an architectural point of view. If this function isn't a jQuery-specific function (e.g. doesn't use or operate on jQuery objects), then it really doesn't belong on the jQuery object. It's a general purpose function and should be defined in a more general purpose way, either as a singleton global function or, if you have multiple types of related functions like this, put it on its own namespace object.

这说,在很多情况下,你可以创建自己的闭包并定义自己的函数供你自己的代码使用,而不必把它们放在一起在全局命名空间或命名空间对象上。因此,除非这些函数必须可以从任何地方全局访问,否则它们不需要继续使用jQuery对象。

This said, in many cases, you can just create your own closure and define your own functions for use by your own code without having to put them either in the global namespace or on a namespace object. So, unless these functions must be globally accessible from anywhere, they don't need to go on the jQuery object.

例如,假设你有两个jQuery插件 - 想要共享一个共同功能的方法。显然,这个函数必须在这两个插件方法之外定义。但是,它不必在全球范围内定义。我可以放入一个闭包并以这种方式共享:

For example, let's suppose you had two jQuery plugin-methods that wanted to share a common function. Obviously, this function has to be defined outside of these two plug-in methods. But, it does not have to be defined globally. I can be put in a closure and shared that way:

// define closure
(function() {

    // declare shared function in a closure
    function commonFunc() {
        // common code here
    }

    // define first plugin method
    jQuery.fn.plugin1 = function() {
        // do stuff
        // ....

        // use common function
        commonFunc();
    };

    // define other plugin method
    jQuery.fn.plugin2 = function() {
        // do different stuff
        // ....

        // use common function
        commonFunc();
    };

})();

这篇关于为什么人们将自己的自定义/用户函数添加到jQuery对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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