Do you use 'this' in front of instance variables?

前端 未结 17 2136
猫巷女王i
猫巷女王i 2020-12-19 01:08

When accessing instance variables or properties of a class from within the class itself, do you prepend them with \"this.\"?

17条回答
  •  天涯浪人
    2020-12-19 01:55

    Generally yes, I do. Communicating scope is an important aspect of readability. It distinguishes it from local variables, static methods, etc. It also communicates that the definition is "nearby".

    Oh, and I only do it for public methods/properties. Those tend to be capitalized so this.Thing looks right. The internal view looks like the external view (myInstance.Thing). Private properties are often lowercase and so it's a less attractive idea for those.

    It's not strictly necessary of course, and some people prefer it more terse. But it provides hints to me and other devs who might look at the code.

提交回复
热议问题