新的JavaScript“Symbol"有哪些可能的使用场景?数据类型? [英] What are the possible usage scenarios for the new JavaScript "Symbol" datatype?

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

问题描述

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

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/Symbolhttp://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";

但是,由于 Symbol('a') 总是返回唯一值(对象)并且:

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

符号在 for...in 迭代中不可见.

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..inObject.getOwnPropertyNames()<时不可见/code> 或 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“Symbol"有哪些可能的使用场景?数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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