admin管理员组文章数量:1321057
Can someone explain the difference between using a $ when defining variables in jQuery and not. Does it have any impact on performance?
var $someVar = $("a");
var someVar = $("a");
Also, what is the difference in calling variables with and without $(someVar)
For example:
$(someVar).html();
someVar.html();
Can someone explain the difference between using a $ when defining variables in jQuery and not. Does it have any impact on performance?
var $someVar = $("a");
var someVar = $("a");
Also, what is the difference in calling variables with and without $(someVar)
For example:
$(someVar).html();
someVar.html();
Share
Improve this question
edited Dec 19, 2024 at 15:29
dumbass
27.2k4 gold badges36 silver badges73 bronze badges
asked Jun 30, 2010 at 5:38
RyanLynchRyanLynch
3,0074 gold badges37 silver badges48 bronze badges
3 Answers
Reset to default 3In your first snippet, there is no difference between those two. It's just a "notification" that this variable is holding a wrapped set of jQuery objects, which is monly used by some developers.
In your second snippet you are actually doing two different things. You are wrapping someVar
into a jQuery object and then access a jQuery method
(html()).
In this example someVar
could contain a DOM element or a selector string like "#some_element_id".
The other line assumes that someVar
already IS a jQuery object, otherwise this call would fail.
There is no performance difference, it's just an identifier. $foo
is often used to refer to jquery objects by experienced authors so as to not confuse them with non-jquery objects in plex scripts.
As for the $()
wrapping, you can reference a DOMElement and wrap it in jQuery, eg
var e = document.body; $(e).hide()
If we tried e.hide()
there would be no defined method as the body element doesn't have that method, it's only provided by the jQuery prototype chain.
$somevar
and somevar
are both the same . It is a convention to use a $ before a jquery variale so that you know that it is a jquery wrapped object .
( btw, $ were introduced in javascript to distinguish machine generated code for human written code )
However , $(somevar) and somevar are pletely different .
$(somevar) -> You are calling the Jquery function and passing it somevar . You can also read this as Jquery(somevar) . So if somevar is referring to a dom id , the dom object would be wrapped by jquery and returned .
本文标签: javascriptWhat does it mean when a variable name contains a dollar signStack Overflow
版权声明:本文标题:javascript - What does it mean when a variable name contains a dollar sign? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742092849a2420370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论