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?
仅有的幸福2017-07-05 10:55:02
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.
迷茫2017-07-05 10:55:02
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
ringa_lee2017-07-05 10:55:02
_
means it has no special meaning. For example, in functions like array map
forEach
var goAhead = arr => arr.map(
// map 的第一个参数是函数
(_, idx, its) => {
return its[idx + 1] || its[0];
}
);
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
_
setTimeout(_ => {
console.log('。。。括号都懒掉了');
}, 200);
_ to organize various functional tools, such as
_.forEach
_.map
_ 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)
$, 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.
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 "__*" } )
某草草2017-07-05 10:55:02
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.
天蓬老师2017-07-05 10:55:02
$ is jquery
_ is underscore
In addition, there is a convention at the beginning of _ to indicate unused variables
滿天的星座2017-07-05 10:55:02
Because it’s short!
var asdfasdfawdfsakdfaskjf
var $
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 $
than jQuery
!
女神的闺蜜爱上我2017-07-05 10:55:02
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,
fn = () => [1, 2]
// fn是一个函数,返回两个数
// 假如我只对第二个数感兴趣,则可以用变量_来存放第一个数
[_, a] = fn()
// 现在_ = 1, a = 2
As for $
, it is used more in jquery
and is used to replace jQuery
, making it easier for you to type
给我你的怀抱2017-07-05 10:55:02
No special requirements
It’s just personal habits
You can write whatever you want
女神的闺蜜爱上我2017-07-05 10:55:02
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_