属性和方法名称的下划线前缀仅仅是一种约定吗? [英] Is the underscore prefix for property and method names merely a convention?

查看:46
本文介绍了属性和方法名称的下划线前缀仅仅是一种约定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript 中的下划线前缀是否只是一种约定,例如在 Python 私有类方法中是这样?

来自 2.7 Python 文档:

<块引用>

私有"实例变量除了从内部无法访问Python 中不存在对象.但是,有一个约定是后跟大多数 Python 代码:名称以下划线为前缀(例如_spam) 应被视为 API 的非公开部分(无论是是函数、方法或数据会员).

这也适用于 JavaScript 吗?

以这段 JavaScript 代码为例:

function AltTabPopup() {this._init();}AltTabPopup.prototype = {_init:函数(){...}}

此外,使用下划线前缀的变量.

<代码> ...this._currentApp = 0;this._currentWindow = -1;this._thumbnailTimeoutId = 0;this._motionTimeoutId = 0;...

只有约定?或者下划线前缀后面还有更多?

<小时>

我承认我的问题与这个问题非常相似,但它没有关于 JavaScript 中下划线前缀的重要性的一个更聪明的人.

解决方案

欢迎来到 2019!

似乎有一个提议来扩展类语法允许将 # 前缀变量设为私有已被接受.Chrome 74 随附此支持.

_ 前缀变量名按照惯例被认为是私有的,但仍然是公开的.

<块引用>

这种语法试图既简洁又直观,尽管它与其他编程语言有很大不同.

为什么在所有 Unicode 代码点中选择符号 #?

  • @ 是最初的最爱,但它被装饰者采用了.TC39 考虑过交换装饰器和私有状态标记,但委员会决定遵循转译器用户的现有用法.
  • _ 会导致与现有 JavaScript 代码的兼容性问题,这在很长一段时间内都允许在标识符或(公共)属性名称的开头使用 _.

该提案于 2017 年 7 月进入第 3 阶段.从那时起,人们对各种替代方案进行了广泛的思考和长时间的讨论.最后,这个思考过程和持续的社区参与导致对该存储库中的提案重新达成共识.基于这一共识,该提案的实施正在向前推进.

参见 https://caniuse.com/#feat=mdn-javascript_classes_private_class_fields>

Is the underscore prefix in JavaScript only a convention, like for example in Python private class methods are?

From the 2.7 Python documentation:

"Private" instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member).

Does this also apply to JavaScript?

Take for example this JavaScript code:

function AltTabPopup() {
    this._init();
}

AltTabPopup.prototype = {
    _init : function() {
        ...
    }
}

Also, underscore prefixed variables are used.

    ...
    this._currentApp = 0;
    this._currentWindow = -1;
    this._thumbnailTimeoutId = 0;
    this._motionTimeoutId = 0;
    ...

Only conventions? Or is there more behind the underscore prefix?


I admit my question is quite similar to this question, but it didn't make one smarter about the significance of the underscore prefix in JavaScript.

解决方案

Welcome to 2019!

It appears a proposal to extend class syntax to allow for # prefixed variable to be private was accepted. Chrome 74 ships with this support.

_ prefixed variable names are considered private by convention but are still public.

This syntax tries to be both terse and intuitive, although it's rather different from other programming languages.

Why was the sigil # chosen, among all the Unicode code points?

  • @ was the initial favorite, but it was taken by decorators. TC39 considered swapping decorators and private state sigils, but the committee decided to defer to the existing usage of transpiler users.
  • _ would cause compatibility issues with existing JavaScript code, which has allowed _ at the start of an identifier or (public) property name for a long time.

This proposal reached Stage 3 in July 2017. Since that time, there has been extensive thought and lengthy discussion about various alternatives. In the end, this thought process and continued community engagement led to renewed consensus on the proposal in this repository. Based on that consensus, implementations are moving forward on this proposal.

See https://caniuse.com/#feat=mdn-javascript_classes_private_class_fields

这篇关于属性和方法名称的下划线前缀仅仅是一种约定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆