直接在jQuery对象上定义实用程序函数有什么好处吗? [英] Is there any benefit to defining a utility function directly on the jQuery object?

查看:74
本文介绍了直接在jQuery对象上定义实用程序函数有什么好处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直接在jQuery对象上定义实用程序函数是否有任何特定的好处:

Are there any specific benefits derived from defining a utility function directly on the jQuery object:

例如,给定以下两个结构:

For instance, given the following two constructs:

$.someUtility = function(){
   //do some something with jQuery
}


var someUtility = function(){
   //do some something with jQuery
}

我是否有任何具体原因要在第二个例子中使用第一个例子?

Is there any specific reason I would want to use the first example over the second?

快速更新:
我不需要做任何链接,我的实用程序不是传统意义上的插件;它不会对jQuery选择器执行任何操作。

Quick Update: I don't need to do any chaining, and my utility is not a plugin in the traditional sense; It will not perform any operations on a jQuery selector.

推荐答案

你只是借用全局jQuery函数对象使您的函数可用于其他脚本,而不会进一步污染全局变量环境。

You're simply borrowing the global jQuery function object to make your function available to other scripts without further polluting the global variable environment.

如果您没有其他依赖的脚本该函数可以使它成为局部变量而不会有任何缺点,除非您污染了本地变量环境。

If you have no other scripts that rely on that function, you could make it a local variable with no disadvantage except that you'd be polluting your local variable environment.

这篇关于直接在jQuery对象上定义实用程序函数有什么好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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