新JavaScript“符号”的可能使用场景是什么?数据类型? [英] What are the possible usage scenarios for the new JavaScript "Symbol" datatype?

查看:153
本文介绍了新JavaScript“符号”的可能使用场景是什么?数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是偶然发现了新的文档(针对ES6提出,但已经在Firefox,Chrome和Opera中实现)的JavaScript数据类型,符号

I just stumbled upon the documentation for the new (proposed for ES6, but already implemented in Firefox, Chrome & Opera) datatype in JavaScript, Symbol:

https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-symbol-objects

我正在读它,但我想不出可能的使用场景。

I'm reading about it, but I just can't think of a possible usage scenario.

文档说:


符号是唯一且不可变的数据类型,可用作对象属性的标识符。

A symbol is a unique and immutable data type and may be used as an identifier for object properties.

好的,好的,假设我按照文档说的那样做:

OK, fine, let's say I do as the documentation says:

obj[Symbol("a")] = "a";

但是,因为符号('a')总是返回唯一值(对象)并且:

but, since Symbol('a') always returns unique value (object) and:


迭代中的符号在...中不可见。

Symbols are not visible in for...in iterations.

如何从 obj 中检索我的财产?

var obj = { normalProperty: 'just a string' };
obj[Symbol('a')] = 'a';

document.getElementById('retrieve').addEventListener('click', function() {
  document.write('Stringified object:' + JSON.stringify(obj) + '<br/><br/>');
  document.write('Trying to get Symbol-property value, aaaand...: <br/>');
  document.write(obj[Symbol('a')]); // undefined
}, false);

<button id="retrieve">Retrieve obj's property</button>

当然,您可以这样检索:

Of course, you can retrieve it like this:

var x = Symbol('a');
obj[x] = 'a';
obj[x]; // "a"

但这样做的目的是什么?

提前致谢:)

推荐答案

阅读文档后并且使用chrome中的 Symbol 类型玩一下,看来 Symbol 是一种定义名称的方法 - 不是值 - 以及使用符号定义的属性使用 for..in Object.getOwnPropertyNames() JSON.stringify()使符号对元数据属性有用:

After reading the documentation and playing a little bit with this Symbol type in chrome, it appears that a Symbol is a way to define a name -- not a value --, and the fact that properties defined using symbols are not visible using for..in, Object.getOwnPropertyNames() or JSON.stringify() makes symbols useful for metadata properties:

// define metadata symbols
var Metadata = {
        Date: Symbol('Message date')
};

var email = function(recipient, message) {
    this.Recipient = recipient;
    this.Message = message;
    this[Metadata.Date] = new Date();
};

var email1 = new email('@Me', 'test');
JSON.stringify(email1);
// {
//    Recipient: '@Me',
//    Message: 'test'
// }

// Date is still accessible using
email1[Metadata.Date];
// Thu Nov 27 2014 16:50:00 GMT+0000

// Debugging in Console:
// {
//    Recipient: '@Me',
//    Message: 'test'
//    Symbol(Message date): Thu Nov 27 2014 16:50:00 GMT+0000
// }

使用 Symbol.for 函数可以使符号全局化,因此元数据名称可以创建一次并在所有项目文件中使用。

Symbols can be made global using the Symbol.for function, so metadata names can be created once and used across all project files.

使用符号访问值需要在创建时引用该符号。每次调用 Symbol()都会创建一个新的,即使使用相同的描述:

Accessing the value using a symbol requires having a reference to the symbol when created. Each call to Symbol() creates a new one even if the same description is used:

var a = Symbol('a');
var b = Symbol('a');
a != b
// and
a != Symbol('a')

但是,使用 Symbol.for 创建一个符号,它将在全局注册表中注册,并且描述成为一个键,意味着只有一个具有相同的符号密钥将存在于全局注册表中:

but, creating a symbol using Symbol.for, it will be registered in a global registry and the description becomes a key, meanong only one symbol with the same key will exist in the global registry:

var a = Symbol.for('a');
var b = Symbol.for('a');
a == b
// and
a == Symbol.for('a')

这篇关于新JavaScript“符号”的可能使用场景是什么?数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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