Home  >  Article  >  Web Front-end  >  What is the difference between adding $ and not adding $ to jquery variables?

What is the difference between adding $ and not adding $ to jquery variables?

coldplay.xixi
coldplay.xixiOriginal
2020-12-23 11:52:192453browse

There is no difference between adding $ to a jquery variable and not adding $. When using jquery, if a variable is wrapped by [$();], it means that the variable needs to be packaged into a jquery object before it can be used.

What is the difference between adding $ and not adding $ to jquery variables?

The operating environment of this article: Windows 7 system, Dell G3 computer, jquery version 3.2.1.

Recommended: jquery video tutorial

There is no difference between adding $ and not adding $ to jquery variables, it’s just a habit.

1. The starting characters in the variable naming rules can be letters, underscores (_), and dollar signs ($). However, many js libraries like to use $ as a global variable mark, and jquery is no exception. , plus $, as a jquery variable mark to make it easier to distinguish. Therefore, when naming a jquery object, add $ in front, and you will know it is a jquery object at a glance.

2. When using jquery, if a variable is wrapped by $();, it means that the variable needs to be packaged into a jquery object before it can be used.

3. Variables created in jquery’s own scope do not need to be wrapped with $();. To give a very simple example: for example, a this pointer in js, if this pointer needs to be in If used in jQuery, then you need to use $(); to wrap it into an object in jQuery, and you need to write it like this: $(this).

4. If one variable is named $xxx, and another variable is named xxx, then this is a good coding habit to distinguish the variables created using jquery. and variables created by javascript itself.

Related free learning recommendations: js video tutorial

The above is the detailed content of What is the difference between adding $ and not adding $ to jquery variables?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn