javascript What is the point of prefixing function names with '_' React/React-native ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34305905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:31:06  来源:igfitidea点击:

What is the point of prefixing function names with '_' React/React-native ?

javascriptreactjsreact-native

提问by Nash Vail

So as per convention since JavaScript doesn't have access modifiers I have to prefix "private" function names with an underscore(_). But I am a little confused here, say I am writing a class in C++ or Java that has two functions one that performs internal calculations and the second that returns the result of that internal calculation.

So as per convention since JavaScript doesn't have access modifiers I have to prefix "private" function names with an underscore(_). But I am a little confused here, say I am writing a class in C++ or Java that has two functions one that performs internal calculations and the second that returns the result of that internal calculation.

So, I have these two functions

So, I have these two functions

performInternalSecretCalculation();
getResult();

the performInternalSecretCalculation()is supposed to be private since I don't want other classes to worry about how the class handles calculation and hence I don't export this function. Whereas getResult()is something that will be used by other classes and hence I make it public and export this function.

the performInternalSecretCalculation()is supposed to be private since I don't want other classes to worry about how the class handles calculation and hence I don't export this function. Whereas getResult()is something that will be used by other classes and hence I make it public and export this function.

But in case of React classes I am not exporting any functions all the functions defined inside a react class are used within it. So what is the differentiator? When should I prefix an underscore before a function's name?

But in case of React classes I am not exporting any functions all the functions defined inside a react class are used within it. So what is the differentiator? When should I prefix an underscore before a function's name?

回答by purii

It is just a naming convention, used by some developers for internal methods to separate them from the lifecycle methods of react.

It is just a naming convention, used by some developers for internal methods to separate them from the lifecycle methods of react.

Lifecycle methods

Lifecycle methods

  • constructor
  • getChildContext
  • componentWillMount
  • componentDidMount
  • componentWillReceiveProps
  • shouldComponentUpdate
  • componentWillUpdate
  • componentDidUpdate
  • componentWillUnmount
  • constructor
  • getChildContext
  • componentWillMount
  • componentDidMount
  • componentWillReceiveProps
  • shouldComponentUpdate
  • componentWillUpdate
  • componentDidUpdate
  • componentWillUnmount

List is borrowed from the AirBnB Style Guide.

List is borrowed from the AirBnB Style Guide.

Airbnb React/JSX Style Guide

Airbnb React/JSX Style Guide

I prefer the Style Guide from AirBnB. They suggest not to use underscore prefix. I force instead a clean order for the methods.

I prefer the Style Guide from AirBnB. They suggest not to use underscore prefix. I force instead a clean order for the methods.