javascript - I see some experts writing code that use "$" and "_" alone to represent variables. When should these two characters be used alone?
ringa_lee
ringa_lee 2017-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?

ringa_lee
ringa_lee

ringa_lee

reply all(9)
仅有的幸福

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

ringa_lee

_ 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

_

Or some are lazier. . .

setTimeout(_ => {
    console.log('。。。括号都懒掉了'); 
}, 200);

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.

Peter_Zhu

$ is jquery
_ is underscore

In addition, there is a convention at the beginning of _ to indicate unused variables

滿天的星座

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!

女神的闺蜜爱上我

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

给我你的怀抱

No 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_

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template