search

Home  >  Q&A  >  body text

javascript - I see some experts writing code that use "$" and "_" alone to represent variables. When should these two characters be used alone?

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_leeringa_lee2728 days ago1086

reply all(9)I'll reply

  • 仅有的幸福

    仅有的幸福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.

    reply
    0
  • 迷茫

    迷茫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

    reply
    0
  • ringa_lee

    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

    _

    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.

    reply
    0
  • 某草草

    某草草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.

    reply
    0
  • 天蓬老师

    天蓬老师2017-07-05 10:55:02

    $ is jquery
    _ is underscore

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

    reply
    0
  • 滿天的星座

    滿天的星座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!

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我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

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-07-05 10:55:02

    No special requirements
    It’s just personal habits
    You can write whatever you want

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我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_

    reply
    0
  • Cancelreply