Underscore prefix for property and method names in JavaScript

后端 未结 6 1866
误落风尘
误落风尘 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... */ }
    

提交回复
热议问题