开发者

What is the official correct way to declare jQuery variables?

I am trying to learn jQuery. I keep seeing people using different methods of declaring variables.

I have seen var variable;, v开发者_StackOverflow中文版ar $variable;, and $variable;. Which one (if any of these) is the official correct way to declare jQuery variables.


You should always declare a variable with var (whatever the scope, unless creating it on an object), implied globals are bad, don't do it.

As for variable vs. $variable, there is no official stance, just have a convention that you and your team stick to.

As a general rule some follow, (I don't follow this, some do), use variable for variables that aren't jQuery objects and $variable for variables that are, those who use this typically find that seeing $something and knowing immediately it's a jQuery object to be handy.


Use var to define variables to ensure that they are scoped properly, usually locally within a function. My convention is to use $variable when the variable holds a jQuery object (result of a selector) and variable for everything else.

BTW -- they aren't jquery variables, but rather javascript variables. JQuery is just another (albeit the most popular at present) javascript framework, not a language in and of itself.


There is no official correct way to declare variables for jQuery..

They are all javascript variables, and these are the rules for javascript variables

Using the $ at the beginning is a convention that the variable is holding a jQuery object (since the jquery uses the $ sign for the function wrapper)

The diffeernce of using var or not, is in the scope of the variable. Using var declares it local, not using var adds it to the global scope.


Nick already explained why you should use var.

The reason behind variable and $variable is that $ is associated with jQuery and should imply that this variable holds a jQuery object. To avoid unnecessary function calls to jQuery like $(variable) where variable actually already holds a jQuery object.


There is no such thing as a "jQuery variable", they are Javascript variables. When learning jQuery you have to learn some Javascript, as jQuery is just a library for Javascript.

You declare a variable using the var keyword. You can also use a variable without declaring it, and it will implicitly be declared as a global variable, but that is not recommended. You should declare variables in the scope where they are used.

Sometimes putting a $ sign as the first character of a variable name is used to indicate that it's intended for containing a reference to a jQuery object, however there is no official recommendation, and the language itself doesn't care what you name your variables.


The ones with var are local variables, the one without it is global. So it's not just a matter of style. Apart from that use anything you like, personally I haven't seen "$" prefixed variables, but maybe they are useful if you mix a lot of jQuery and standard JavaScript code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜