JavaScript ES6类中的私有属性

本文翻译自:Private properties in JavaScript ES6 classes

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楼

参考:https://stackoom.com/question/1Uxs6/JavaScript-ES-类中的私有属性


#2楼

Depends on whom you ask :-) 取决于你问的人 :-)

No private property modifier is included in the Maximally minimal classes proposal which seems to have made it into the current draft . 似乎已纳入当前草案的“ 最大最小类”提议中未包含private属性修饰符。

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


#3楼

Update: A proposal with nicer syntax is on its way. 更新: 语法更好提案正在实施中。 Contributions are welcome. 欢迎捐款。


Yes, there is - for scoped access in objects - ES6 introduces Symbol s . 是的,对于对象中的作用域访问, ES6引入了Symbol s

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: 但是您可以创建对属性的私有访问,如下所示:

(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: https://mail.mozilla.org/pipermail/es-discuss/2014-January/035604.html and https://stackoverflow.com/a/22280202/1282216 . For a longer discussion about Symbols and privacy see: https://curiosity-driven.org/private-properties-in-javascript ) (关于在早期版本的ES6规范中可以使用Symbols来确保隐私的建议,但现在不再适用: https : //mail.mozilla.org/pipermail/es-discuss/2014-January/035604。 htmlhttps://stackoverflow.com/a/22280202/1282216 。有关符号和隐私的详细讨论,请参见: https : //curiosity-driven.org/private-properties-in-javascript


#5楼

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

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; }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值