Underscore prefix for property and method names in JavaScript

后端 未结 6 1862
误落风尘
误落风尘 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: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.

提交回复
热议问题