javascript ECMAScript 6中Symbol的用途是什么? [英] What is the use of Symbol in javascript ECMAScript 6?

查看:139
本文介绍了javascript ECMAScript 6中Symbol的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javascript(ECMASCRIPT6)中符号的用途是什么?

What is the use of Symbol in javascript (ECMASCRIPT6)?

为什么下面的示例返回false?

Why does the example below return false?


const symbol1 = Symbol();

console.log(Symbol('foo') === Symbol('foo'));
// expected output: false

推荐答案

Symbol用于创建完全唯一的一种标识符.正是用于您列出的示例.

Symbol is used to create a totally unique, one-of-a-kind identifier. It's use is precisely for the example you list.

即使使用相同的字符串调用Symbol,实例也将不同.这允许不同的库(可以同时使用)来定义可以同时使用的密钥.

Even if you call Symbol with the same string, the instances will be different. This allows different libraries (which may be used at the same time) to define keys which may be used at the same time.

例如,假设两个库使用通用名称在windowglobal上定义了某些内容(或为示例,假全局door):

For example, imagine two libraries using a common name to define something on window or global (or for illustration, the fake global door):

const door = {};
// from library 1
door.cake = () => console.log('chocolate');
// from library 2
door.cake = () => console.log('vanilla');

// your code
door.cake();

在此示例中,第一个库代码丢失了,因为它无意中被赋予了与第一个库相同的名称.

In this example, the first libraries code is lost because it was unintentionally given the same name as the first.

现在,如果它们都使用Symbol,那么即使它们的名称相同,您仍然可以访问两者(假设它们以某种方式导出Symbol):

Now, if they both use Symbol, then even if they are named the same, you can still access both (assuming they export Symbol somehow):

const door = {};
// library 1
const cake1 = Symbol('cake');
door[cake1] = () => console.log('chocolate');
// library 2
const cake2 = Symbol('cake');
door[cake2] = () => console.log('vanilla');
// your code
door[cake1]();
door[cake2]();

两者仍然可以访问.

有点过分简化,但这说明了这一点.

That is a bit of an oversimplification, but it illustrated the point.

在更实际的用法中,这些用于诸如导入模块之类的东西.这些模块可能以相同的名称结尾,但这没关系,因为它们将具有与它们相关联的唯一符号,这使得它们可以唯一访问,只要您具有Symbol对象即可.

In a more practical usage, these are used for things such as importing modules. The modules may end up with the same name, but that's okay because they'll have unique symbols associated with them, which makes them uniquely accessible as long as your have the Symbol objects.

关于何时自己使用它们……可能会非常少见.只要有一种提供Symbol的方式,您将主要希望使用它们,但需要其他方式来保持唯一性.我仅在少数狭窄的情况下直接使用了这些情况,在这些情况下,创建的元素可能会以相同的方式结束.

As for when to use them yourself... it's probably going to be pretty rare. You'll mainly want to use them any time you have a way to provide the Symbol but need other things to remain unique. I've only used these directly in a few narrow circumstances where the created element may end up the same.

例如,如果您使用名称作为键来创建对象,则可能会有重复的名称.没有符号,对象将相互覆盖.有了符号,它们都将保留.

For example, if you were making an object using names as the key, you might have duplicate names. Without symbols, the objects would override one other. With symbols, they'll all remain.

const people = {};
people[Symbol('bob')] = { name: 'Bob Smith' };
people[Symbol('bob')] = { name: 'Bob Jones' };

这篇关于javascript ECMAScript 6中Symbol的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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