Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the underscore "_" in JavaScript? [duplicate]

Tags:

I'm doing a redux tutorial, and I saw a call like this:

this._render(); 

and it's defined elsewhere as:

_render() {     .... }  

What is the underscore "_"? Why is it used?

like image 786
userden Avatar asked Jun 24 '17 08:06

userden


2 Answers

This is convention of private methods and variables. In JavaScript there is no real privacy of classes.

It means that you should not use these method (starting with "_") out of your object. Of course technically you can, but "_" means that you should not.

like image 160
Daniel Avatar answered Sep 25 '22 22:09

Daniel


Underscore (_) is just a plain valid character for variable/function name, it does not bring any additional feature.

However, it is a good convention to use underscore to mark variable/function as private. You can check Underscore prefix for property and method names in JavaScript for some previous discussion.

like image 21
shaochuancs Avatar answered Sep 25 '22 22:09

shaochuancs