为什么有些人写一个像$(函数($)的函数 [英] why some people write a function like $(function( $ )

查看:81
本文介绍了为什么有些人写一个像$(函数($)的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道像 $(function($)这样的代码没有任何意义,但我在各个地方找到了这种代码,包括 todomvc

I know that code like $(function( $ ) does not make any sense, but I have find this sort of code at various places including todomvc.

那里编写像 jQuery(function($)这样的函数来解决任何其他库使用的 $ 的任何潜在冲突的原因,但不是 $ function($)

There is a reason writing functions like jQuery(function( $ ) to resolve any potential conflict of $ used by any other library, but not $function($).

推荐答案

没有使用理由

$(function($))...

如果您在行的开头使用美元符号,那么它依赖于它是一个jQuery对象,所以如果稍后将jQuery对象作为参数传递给避免冲突,你为什么不首先使用它?现在为时已晚......

If you use the dollar sign in the beginning of the line you rely on that it's a jQuery object, so if you pass the jQuery object later on as a parameter to avoid conflicts, why didn't you use it on the first place? It's too later for that now...

使用它的正确方法是:

(function($){ // The dollar variable is a parameter.
   $(function(){ // You use the $ variable only inside the function.
   });
})(jQuery); // jQuery is passed as a parameter.

$.somePrototypeFunctionHere(); // Here the dollar variable can be something else.

这篇关于为什么有些人写一个像$(函数($)的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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