在jQuery中,使用美元符号VS var有什么区别? [英] In jQuery what is the difference between using the dollar sign VS var?

查看:137
本文介绍了在jQuery中,使用美元符号VS var有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与一个在jQuery中声明变量时从不使用var的开发人员合作.像这样:

I'm working with a developer that never uses var when declaring variables in jQuery. Something like:

$x = "hello";
$y = "world";
$a = x + " " + y;

jQuery对此没有问题,他已经做了一段时间了.我一直都用var:

jQuery doesn't have a problem with this and he's been doing this for awhile. I've always used var:

var x = "hello";
var y = "world";
var a = x + " " + y;

我们遇到了一个问题,其中美元符号引起了问题(那是一年前的事,所以我不记得了.)

We have run into one issue where the dollar sign caused an issue (it was a year ago so I can't recall the issue).

有人知道或有关于两者之间区别的信息吗?为什么一个更好或更应该被另一个使用?

Does anyone know or have info on the difference between the two and why one is better or should be used over the other?

我已经做了一些Google搜索,但是他们都谈论var $x; vs var x;

I've done some Google searching but they all talk about var $x; vs var x;

推荐答案

使用美元符号不能代替var.变量名中的$与任何其他字符相同. $通常位于包含jQuery对象的变量之前.即:

Using a dollar sign isn't a replacement for var. $ in a variable name is the same as any other character. $ is often prepended to variables that contain a jQuery object. I.E:

var el = document.getElementById('id');

对:

var $el = $('#id');

因此,它主要用于区分.它对脚本的执行没有影响.

So it's used primarily to differentiate. It makes no difference to the execution of your script.

var$完全无关.实际上,在全球范围内:

var and $ in a reference are completely unrelated. In fact, in the global scope:

var $el = $('#id');

$el = $('#id')

这篇关于在jQuery中,使用美元符号VS var有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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