Underscore prefix for property and method names in JavaScript

后端 未结 6 1725
误落风尘
误落风尘 2020-11-22 16:26

Is the underscore prefix in JavaScript only a convention, like for example in Python private class methods are?

From the 2.7 Python documentation:

相关标签:
6条回答
  • 2020-11-22 16:35

    import/export is now doing the job with ES6. I still tend to prefix not exported functions with _ if most of my functions are exported.

    If you export only a class (like in angular projects), it's not needed at all.

    export class MyOpenClass{
    
        open(){
             doStuff()
             this._privateStuff()
             return close();
        }
    
        _privateStuff() { /* _ only as a convention */} 
    
    }
    
    function close(){ /*... this is really private... */ }
    
    0 讨论(0)
  • 2020-11-22 16:41

    "Only conventions? Or is there more behind the underscore prefix?"

    Apart from privacy conventions, I also wanted to help bring awareness that the underscore prefix is also used for arguments that are dependent on independent arguments, specifically in URI anchor maps. Dependent keys always point to a map.

    Example ( from https://github.com/mmikowski/urianchor ) :

    $.uriAnchor.setAnchor({
      page   : 'profile',
      _page  : {
        uname   : 'wendy',
        online  : 'today'
      }
    });
    

    The URI anchor on the browser search field is changed to:

    \#!page=profile:uname,wendy|online,today
    

    This is a convention used to drive an application state based on hash changes.

    0 讨论(0)
  • 2020-11-22 16:43

    That's only a convention. The Javascript language does not give any special meaning to identifiers starting with underscore characters.

    That said, it's quite a useful convention for a language that doesn't support encapsulation out of the box. Although there is no way to prevent someone from abusing your classes' implementations, at least it does clarify your intent, and documents such behavior as being wrong in the first place.

    0 讨论(0)
  • 2020-11-22 16:48

    JavaScript actually does support encapsulation, through a method that involves hiding members in closures (Crockford). That said, it's sometimes cumbersome, and the underscore convention is a pretty good convention to use for things that are sort of private, but that you don't actually need to hide.

    0 讨论(0)
  • 2020-11-22 16:49

    Welcome to 2019!

    It appears a proposal to extend class syntax to allow for # prefixed variable to be private was accepted. Chrome 74 ships with this support.

    _ prefixed variable names are considered private by convention but are still public.

    This syntax tries to be both terse and intuitive, although it's rather different from other programming languages.

    Why was the sigil # chosen, among all the Unicode code points?

    • @ was the initial favorite, but it was taken by decorators. TC39 considered swapping decorators and private state sigils, but the committee decided to defer to the existing usage of transpiler users.
    • _ would cause compatibility issues with existing JavaScript code, which has allowed _ at the start of an identifier or (public) property name for a long time.

    This proposal reached Stage 3 in July 2017. Since that time, there has been extensive thought and lengthy discussion about various alternatives. In the end, this thought process and continued community engagement led to renewed consensus on the proposal in this repository. Based on that consensus, implementations are moving forward on this proposal.

    See https://caniuse.com/#feat=mdn-javascript_classes_private_class_fields

    0 讨论(0)
  • 2020-11-22 16:51

    JSDoc 3 allows you to annotate your functions with the @access private (previously the @private tag) which is also useful for broadcasting your intent to other developers - http://usejsdoc.org/tags-access.html

    0 讨论(0)
提交回复
热议问题