简体   繁体   English

ECMA6符号原语

[英]ECMA6 Symbols primitive

Currently i started working with JS ECMA6 for my personal web app, but I am not sure about usage of Symbols ie how can I make use of it? 目前,我开始为个人Web应用程序使用JS ECMA6,但是我不确定符号的用法,即如何使用它?

I believe, they are tokens that serve as unique IDs. 我相信,它们是充当唯一ID的令牌。 But I am not sure about its usage in web app development. 但是我不确定它在Web应用程序开发中的用途。

I am currently new to this standard, please provide your suggestions on it. 我目前对该标准还不熟悉,请提供您的建议。

Symbols enable access control for object state. 符号启用对对象状态的访问控制。 Symbols allow properties to be keyed by either string (as in ES5) or symbol. 使用符号可以通过字符串(如ES5中)或符号来键入属性。 Symbols are a new primitive type. 符号是一种新的原始类型。 Optional description parameter used in debugging - but is not part of identity. 调试中使用的可选描述参数-但不是标识的一部分。 Symbols are unique (like gensym), but not private since they are exposed via reflection features like Object.getOwnPropertySymbols. 符号是唯一的(例如gensym),但不是私有的,因为它们通过诸如Object.getOwnPropertySymbols之类的反射功能公开。

var MyClass = (function() {

  // module scoped symbol
  var key = Symbol("key");

  function MyClass(privateData) {
    this[key] = privateData;
  }

  MyClass.prototype = {
    doStuff: function() {
      ... this[key] ...
    }
  };

  return MyClass;
})();

var c = new MyClass("hello")
c["key"] === undefined

It can be referred here . 可以在这里参考 Here are few more links with nicely explained examples.(As already mentioned in comments section.) 这里还有一些链接,其中有很好解释的示例。(如注释部分所述)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM