我正在开发一个包含大量JQuery的项目.例如,JQuery到处都有很多$符号
$(document).ready(function () {
$('input[type=file]').wl_File({
url: '/Admin/PolicyInventory/UploadDocuments',
onFileError: function (error, fileobj) {
$.msg('file is not allowed: ' + fileobj.name, {
header: error.msg + ' Error ',
live: 10000
});
}
});
...
Run Code Online (Sandbox Code Playgroud)
我的问题是,这个美元符号是什么意思?为什么它在所有地方使用,我如何理解和解释它?它让我想起了我在大学学习Scheme的可怕日子,不得不把括号放在任何地方而不知道我为什么要这样做.
T.J*_*der 156
$只是一个捷径jQuery.我们的想法是,所有事情都是用一个全局符号完成的(因为全局名称空间非常拥挤)jQuery,但$如果您愿意,可以使用(因为它更短):
// These are the same barring your using noConflict (more below)
var divs = $("div"); // Find all divs
var divs = jQuery("div"); // Also find all divs, because
console.log($ === jQuery); // "true"
Run Code Online (Sandbox Code Playgroud)
如果您不想使用别名,则不必使用.如果你不想$成为别名jQuery,你可以使用noConflict,库将恢复$到jQuery接管它之前的任何东西.(如果您还使用Prototype或MooTools,则非常有用.)
Den*_*ret 12
它只是一个方便的字符,比"jQuery"更短,更容易阅读.
没有什么特别的,除了它传统上不用于启动变量或函数名称,这降低了风险或名称冲突.
the*_*dox 11
$sign是别名jQuery.一个简短的版本jQuery,一个较少的写机制.
举个例子:(在jQuery中它更复杂)
var yourFunction = function() {
alert('a function');
}
window.Myf = yourFunction;
Run Code Online (Sandbox Code Playgroud)
现在你可以这样打电话yourFunction:
Myf(); // definitely a short syntax
Run Code Online (Sandbox Code Playgroud)
当我们处理库或编程语言时,我们应该注意一些可写性规则.感谢jQuery,他们已经实现了很多选项.您可以使用$或者您可以使用jQuery或者您可以使用_
(function (_) {
_("#wow").click()
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
或者也许你可以做一些奇特的改变,javascript标识符是unicode,所以你可以使用 ?
(function (?) {
?("#wow").click()
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
但其背后的主要思想是,只需按一次键盘就可以打字了 jQuery
在另一边我们有性能......我只是随机打开我的项目并搜索$,我$在一个单独的javascript文件中使用了54 .
$ 是一个字节.
jQuery 是6个字节.
差异很大54*5 = 220字节.
Google 是你的朋友: $ sign JQuery
美元符号只是 JQuery 的别名。
jQuery(document).ready(function(){});
Run Code Online (Sandbox Code Playgroud)
或者
$(document).ready(function(){});
Run Code Online (Sandbox Code Playgroud)