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
964

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(1)
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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!