javascript - I see some experts writing code that use "$" and "_" alone to represent variables. When should these two characters be used alone?
ringa_lee2017-07-05 10:53:02
0
9
1089
Looking at the code written by some experts, "$" and "_" are used alone to represent variables. When should these two characters be used alone? Are there any other habits that are difficult for novices to understand?
Although this is not mandatory, generally for many frameworks, strings starting with _ are used to define internal private properties and methods, and strings starting with $ expose properties or methods to the outside. Classes such as vue are Such.
In addition, for the convenience of writing and generally no conflict, some frameworks will use _ and $ as namespaces or attribute methods: _: underscorejs, lodash, __proto__ (prototype internal attributes), etc. $: jQuery, zepto, $$ (selector), regular $1-$9, etc.
Generally, the prefix _ is a private variable, which is not mandatory; and $ is generally an alias of jQuery, which is often used in jQuery plug-ins and jQuery-based plug-ins
This means that the function body does not use the first parameter, or the first parameter is not important, but if you want to use its, you cannot omit the middle
Also, some JS functional programming libraries use
_ to organize various functional tools, such as _.forEach_.map
The
_ here has no special meaning, the key lies in the content behind ..
Haskell often uses _ to refer to some unimportant function parameters (but they have to be written for pattern matching)
As for
$, it generally refers to DOM libraries such as jQuery or Zepto. It is a convention. Everyone can tell it is jQuery at a glance, and it is also fun to write.
Many people have also mentioned variables starting with an underscore, let me comment.
node’s __dirname indicates the directory where the executed js is located, but why is it named like this with an underline? Because dirname is a very common variable name. If it is not prefixed, it is likely to conflict with the code written by some people. If you add the prefix __, then it will be a variable in another namespace ({x is Variable name | x satisfies "__*" } )
This way it will not interfere with the possible variable name of ordinary dirname.
Because it is convenient and not easy to conflict. Of course, since the emergence of jQuery, some libraries also use $ as variable names. $ and _ are rarely used and are less likely to conflict. They also comply with the specifications of variable naming and are short, so they are used as variable names in some class libraries. _ followed by other letters, such as _this means that the method is private and cannot be accessed by the outside world.
Let’s talk about _ first, when you have to use a variable to get a value, and this variable will not be referenced later (because _ does not make any sense as a variable)
I will also talk about my opinions: - and_ In CSS, it is very common to use text-info like this, using dashes to connect two English words, but in some scenarios, such as vue, sometimes using - will give You reported an error, so I listened to the opinions of some experts, and now I use_
Although this is not mandatory, generally for many frameworks, strings starting with
_
are used to define internal private properties and methods, and strings starting with$
expose properties or methods to the outside. Classes such as vue are Such.In addition, for the convenience of writing and generally no conflict, some frameworks will use
_
and$
as namespaces or attribute methods:_
: underscorejs, lodash, __proto__ (prototype internal attributes), etc.$
: jQuery, zepto, $$ (selector), regular $1-$9, etc.Generally, the prefix
_
is a private variable, which is not mandatory;and
$
is generally an alias of jQuery, which is often used in jQuery plug-ins and jQuery-based plug-ins_
means it has no special meaning. For example, in functions like arraymap
forEach
_
Or some are lazier. . .Also, some JS functional programming libraries use
_
_.map
Theto organize various functional tools, such as
_.forEach_
here has no special meaning, the key lies in the content behind
..
Haskell
often uses
_to refer to some unimportant function parameters (but they have to be written for pattern matching)
As for
$
, it generally refers to DOM libraries such as jQuery or Zepto. It is a convention. Everyone can tell it is jQuery at a glance, and it is also fun to write.
Many people have also mentioned variables starting with an underscore, let me comment.
node
This way it will not interfere with the possible variable name of ordinary dirname.’s
__dirnameindicates the directory where the executed js is located, but why is it named like this with an underline?
Because dirname is a very common variable name. If it is not prefixed, it is likely to conflict with the code written by some people. If you add the prefix__
, then it will be a variable in another namespace (
{x is Variable name | x satisfies "__*" })
Because it is convenient and not easy to conflict.
Of course, since the emergence of
jQuery
, some libraries also use$
as variable names.$
and_
are rarely used and are less likely to conflict. They also comply with the specifications of variable naming and are short, so they are used as variable names in some class libraries._
followed by other letters, such as_this
means that the method is private and cannot be accessed by the outside world.$ is jquery
_ is underscore
In addition, there is a convention at the beginning of _ to indicate unused variables
Because it’s short!
It’s better to knock $ to save trouble
If you are going to write a class library, the simpler the external entry is when using it, the better!
Just like when everyone uses jquery, more people use
$
thanjQuery
!Let’s talk about
_
first,when you have to use a variable to get a value, and this variable will not be referenced later (because
_
does not make any sense as a variable)For example,
As for
$
, it is used more injquery
and is used to replacejQuery
, making it easier for you to typeNo special requirements
It’s just personal habits
You can write whatever you want
I will also talk about my opinions: - and_
In CSS, it is very common to use text-info like this, using dashes to connect two English words, but in some scenarios, such as vue, sometimes using - will give You reported an error, so I listened to the opinions of some experts, and now I use_