admin管理员组文章数量:1279182
I know a bit of JavaScript, and can work fine with jQuery. I just don't get why everything is referenced from $()
. My understanding is that $
is never needed in JavaScript (unlike for example PHP, where every variable is prefixed with $)
.
I've looked through the source code, and it doesn't really make sense. Is it just that $
is the function name (for example, it could have easily have been jQuery()
, but they selected $?
) I assume not, though, as I don't think $
is valid in function names in JavaScript?
I know a bit of JavaScript, and can work fine with jQuery. I just don't get why everything is referenced from $()
. My understanding is that $
is never needed in JavaScript (unlike for example PHP, where every variable is prefixed with $)
.
I've looked through the source code, and it doesn't really make sense. Is it just that $
is the function name (for example, it could have easily have been jQuery()
, but they selected $?
) I assume not, though, as I don't think $
is valid in function names in JavaScript?
2 Answers
Reset to default 18$
is just a global variable that's also a reference to the jQuery function, it's $
on purpose so it's less to type. $
is perfectly valid for a function name in ECMAScript:
function $(){}; alert(typeof $);
Note that if you're using multiple libraries you can use function scope to avoid clashing dollar sign variables, eg:
jQuery.noConflict();
(function($){
$('body').hide();
})(jQuery);
$()
is the same as jQuery()
. Also, $
is a valid function name.
本文标签: javascriptWhyhow is everything () based in jQueryStack Overflow
版权声明:本文标题:javascript - Whyhow is everything $() based in jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741252949a2366134.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论