Skip to content

希望代码中 不要再用 self 指向 this了, 换个名字吧 #18968

@finscn

Description

@finscn

Use Case

用 self 非常不安全. 因为 全局也有个叫 self 的变量. 会引起混淆 , 有时候也会带来一些意外的 bug.

Image

比如如果前面的 const self = this 忘了写, 或者被误删除了. 代码里用到的 self 成员 正好全局 self也有, 就会引起麻烦.

而且有些地方 如果你真的要用 全局的 self (通常web 环境会有这类需求) , 还要给 this起一个其他名字, 就做不到统一了

一般指向 this 的变量名 会用 me, Me , _this , that, thisObj 等等 .
我个人比较喜欢 Me , 因为它短, 且和 this差异比较大(that 和 this 太像了) 而且因为是 大写开头, 给人的第一感觉就是"这不是一个普通的变量, 它有特殊意义'.

Problem Description

.

Proposed Solution

No response

How it works

No response

Alternatives Considered

.

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions