博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript ES6类中的私有属性
阅读量:2380 次
发布时间:2019-05-10

本文共 3660 字,大约阅读时间需要 12 分钟。

本文翻译自:

Is it possible to create private properties in ES6 classes? 是否可以在ES6类中创建私有属性?

Here's an example. 这是一个例子。 How can I prevent access to instance.property ? 如何防止访问instance.property

class Something {  constructor(){    this.property = "test";  }}var instance = new Something();console.log(instance.property); //=> "test"

#1楼

参考:


#2楼

Depends on :-) 取决于 :-)

No private property modifier is included in the which seems to have made it into the . 似乎已纳入的“ 未包含private属性修饰符。

However, there might be , which does allow private properties - and they probably could be used in class definitions as well. 但是,可能会确实允许私有属性的 -并且它们可能也可以在类定义中使用。


#3楼

Update: A is on its way. 更新: 的正在实施中。 Contributions are welcome. 欢迎捐款。


Yes, there is - for scoped access in objects - . 是的,对于对象中的作用域访问, 。

Symbols are unique, you can't gain access to one from the outside except with reflection (like privates in Java/C#) but anyone who has access to a symbol on the inside can use it for key access: 符号是唯一的,除了反射(例如Java / C#中的private)外,您无法从外部访问任何符号,但是内部有权访问符号的任何人都可以将其用于键访问:

var property = Symbol();class Something {    constructor(){        this[property] = "test";    }}var instance = new Something();console.log(instance.property); //=> undefined, can only access with access to the Symbol

#4楼

The answer is "No". 答案是不”。 But you can create private access to properties like this: 但是您可以创建对属性的私有访问,如下所示:

  • Use modules. 使用模块。 Everything in a module is private unless it's made public by using the export keyword. 除非使用export关键字将其公开,否则模块中的所有内容都是私有的。
  • Inside modules, use function closure: 在模块内部,使用函数闭包: :

(The suggestion that Symbols could be used to ensure privacy was true in an earlier version of the ES6 spec but is no longer the case: and . For a longer discussion about Symbols and privacy see: ) (关于在早期版本的ES6规范中可以使用Symbols来确保隐私的建议,但现在不再适用: : 和 。有关符号和隐私的详细讨论,请参见: : )


#5楼

For future reference of other on lookers, I'm hearing now that the recommendation is to use to hold private data. 为了将来供其他参考者参考,我现在听到的建议是使用来保存私有数据。

Here is a more clear, working example: 这是一个更清晰的示例:

function storePrivateProperties(a, b, c, d) {  let privateData = new WeakMap;  // unique object as key, weak map can only accept object as key, when key is no longer referened, garbage collector claims the key-value   let keyA = {}, keyB = {}, keyC = {}, keyD = {};  privateData.set(keyA, a);  privateData.set(keyB, b);  privateData.set(keyC, c);  privateData.set(keyD, d);  return {    logPrivateKey(key) {      switch(key) {      case "a":        console.log(privateData.get(keyA));        break;      case "b":        console.log(privateData.get(keyB));        break;      case "c":        console.log(privateData.get(keyC));        break;      case "d":        console.log(privateData.set(keyD));        break;      default:        console.log(`There is no value for ${key}`)      }    }  }}

#6楼

Short answer, no, there is no native support for private properties with ES6 classes. 简短的答案,不,ES6类不对私有属性提供本机支持。

But you could mimic that behaviour by not attaching the new properties to the object, but keeping them inside a class constructor, and use getters and setters to reach the hidden properties. 但是您可以通过不将新属性附加到对象上,而是将它们保留在类构造函数中,并使用getter和setter来访问隐藏的属性来模仿这种行为。 Note that the getters and setters gets redefine on each new instance of the class. 注意,在类的每个新实例上,getter和setter方法都会重新定义。

ES6 ES6

class Person {    constructor(name) {        var _name = name        this.setName = function(name) { _name = name; }        this.getName = function() { return _name; }    }}

ES5 ES5

function Person(name) {    var _name = name    this.setName = function(name) { _name = name; }    this.getName = function() { return _name; }}

转载地址:http://pbexb.baihongyu.com/

你可能感兴趣的文章
sys文件系统
查看>>
Mysql常用命令大全
查看>>
Linux内核中C编程生僻用法(GNU C)
查看>>
辞职后五险一金怎么处理?
查看>>
几种开源的TCP/IP协议栈对比
查看>>
C语言之断言
查看>>
程序员技术练级攻略
查看>>
#define
查看>>
C语言之if...else PK switch...case
查看>>
关于SVN方面的问题
查看>>
深入理解C语言
查看>>
编程成就:开发人员如何升级
查看>>
如何防止代码腐烂
查看>>
va_start va_end 的使用和原理
查看>>
Linux 中的零拷贝技术,第 2 部分
查看>>
零拷贝技术的研究与实现
查看>>
零拷贝与 sendfile
查看>>
directfb显示中文
查看>>
关于SIGPIPE导致的程序退出
查看>>
setsockopt()函数用法
查看>>