为什么 JQuery 到处都有美元符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10787342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does JQuery have dollar signs everywhere?
提问by Sachin Kainth
I am working on a project with quite a lot of JQuery in it. The JQuery has a lot of $ signs everywhere, for example
我正在开发一个包含大量 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
});
}
});
...
My question is, what does this dollar sign mean? Why is it used all over the place and how do I understand and interpret it? It reminds me of the scary days of when I was learning Scheme at University and had to put brackets everywhere without knowing why I was doing it.
我的问题是,这个美元符号是什么意思?为什么到处都在使用它,我如何理解和解释它?这让我想起了我在大学学习 Scheme 时的可怕日子,我不得不在不知道为什么要这样做的情况下到处加括号。
回答by T.J. Crowder
$
is just a shortcut for jQuery
. The idea is that everythingis done with the one global symbol (since the global namespaces is ridiculously crowded), jQuery
, but you can use $
(because it's shorter) if you like:
$
只是一个快捷方式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"
If you don't want to use the alias, you don't have to. And if you want $
to not be an alias for jQuery
, you can use noConflict
and the library will restore $
to whatever it was before jQuery took it over. (Useful if you also use Prototype or MooTools.)
如果您不想使用别名,则不必使用。如果您不想$
成为 的别名jQuery
,则可以使用noConflict
该库,该库将恢复$
到 jQuery 接管之前的状态。(如果您还使用 Prototype 或 MooTools,则很有用。)
回答by Denys Séguret
It's just a convenient character, shorter to type and easier to read than "jQuery".
它只是一个方便的字符,比“jQuery”更短,更容易阅读。
There is nothing special except that it's traditionally not used to start a variable or function name, which reduces the risk or name collision.
没有什么特别之处,只是它传统上不用于启动变量或函数名称,这降低了风险或名称冲突。
回答by thecodeparadox
$
sign is an alias for jQuery
. A short version of jQuery
, a less write mechanism.
$
sign 是 的别名jQuery
。的简短版本jQuery
,更少的写入机制。
Just for an example: (in jQuery it's more complicated)
举个例子:(在jQuery中它更复杂)
var yourFunction = function() {
alert('a function');
}
window.Myf = yourFunction;
Now you can call yourFunction
like:
现在你可以这样调用yourFunction
:
Myf(); // definitely a short syntax
回答by Ahmet Can Güven
Writability and Performance
可写性和性能
When we are working on library or a programming language we should pay attention to some writability rules. Thanks to jQuery they already implemented lots of options. You can use $
or you can use jQuery
or you can use _
当我们在研究库或编程语言时,我们应该注意一些可写性规则。感谢 jQuery,他们已经实现了很多选项。你可以使用$
或者你可以使用jQuery
或者你可以使用_
(function (_) {
_("#wow").click()
})(jQuery);
Or maybe you can do fancy changes, javascript identifiers are unicode so you can use Ω
或者你可以做一些花哨的改变,javascript标识符是unicode,所以你可以使用 Ω
(function (Ω) {
Ω("#wow").click()
})(jQuery);
But the main idea behind it, pressing once to the keyboard is better than typing jQuery
但它背后的主要思想是,按一次键盘比打字好 jQuery
At the other side we have performance... I just randomly opened of my project and search for $
, I used 54 $
in a single javascript file.
另一方面,我们有性能...我只是随机打开我的项目并搜索$
,我$
在单个 javascript 文件中使用了 54 。
$
is a byte.
$
是一个字节。
jQuery
is 6 bytes.
jQuery
是 6 个字节。
The difference is huge 54 * 5 = 220 bytes.
差异是巨大的 54 * 5 = 220 字节。
回答by Kashif
Google is your friend: $ sign JQuery
Google 是你的朋友: $ sign JQuery
Dollar Sign is just an alias for JQuery.
美元符号只是 JQuery 的别名。
jQuery(document).ready(function(){});
OR
或者
$(document).ready(function(){});
回答by Confusion
In javascript, $
(a single dollar character) is a valid variable name. Several frameworks, among which jQuery, have adopted it as a synonym of an object that contain the top-level convenience methods the framework provides.
在 javascript 中,$
(一个美元字符)是一个有效的变量名。有几个框架,其中 jQuery,已经采用它作为包含框架提供的顶级便利方法的对象的同义词。
回答by Lijo
$ sign is used as an alias to Jquery. instead of using jquery.hide,jquery.show every where we can use the alias $ ($.hide) Since we are using this word lot of times. 'Jquery' will not be a convenient way so we are using the alias $. If we want to change it we can change it by noConflict method var Sample=$.noConflict()
$ 符号用作 Jquery 的别名。而不是使用 jquery.hide,jquery.show 我们可以使用别名 $ ($.hide) 因为我们经常使用这个词。'Jquery' 不是一种方便的方式,所以我们使用别名 $. 如果我们想改变它,我们可以通过 noConflict 方法 var Sample=$.noConflict() 改变它