为什么 JavaScript 使用符号而不是字符串? [英] Why does JavaScript use Symbols instead of just strings?

查看:35
本文介绍了为什么 JavaScript 使用符号而不是字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可迭代对象"在 JavaScript 中是具有 Symbol.iterator 属性的对象.例如:

"Iterables" in JavaScript are objects with a Symbol.iterator property. For example:

let x = { 
  [Symbol.iterator]: function * () { 
    yield * [ 1, 2, 3]; 
  } 
};

[ ...x ] // Array [ 1, 2, 3 ]

所以 Symbol.iterator 只是一个约定.

So Symbol.iterator is just a convention.

但是为什么在 string 可以完成类似的工作时发明了 Symbol 这个概念?

But why was this concept, Symbol, invented when a string could have done a similar job?

let x = { 
  iterator: function * () { 
    yield * [ 1, 2, 3 ]; 
  } 
};

// Invalid, but would be equivalent to [ ...x['iterator']() ]
[ ...x ] 

推荐答案

符号在 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol

特别是:

请注意,Symbol("foo") 不会将字符串 "foo" 强制转换为符号.它每次都会创建一个新符号:

Note that Symbol("foo") does not coerce the string "foo" into a symbol. It creates a new symbol each time:

Symbol('foo') === Symbol('foo'); // false

假设我有一些数据要存储在一些广泛可见的对象中.我可以想出一个字符串,比如 "foo",并将其用作将我的数据存储在对象中的键.稍后,我可以使用相同的字符串(硬编码或存储在某处)从对象中取出数据.

Let's say I have some data that I want to store in some widely-visible object. I can come up with a string, say "foo", and use that as the key to store my data in the object. Later on, I can use that same string (either hard-coded or stored somewhere) to get my data back out of the object.

如果我们这样做,有两件事要记住:任何其他代码都可以查找我们的数据,任何其他代码都可以有意或无意地覆盖我们的数据(例如,它们只是这样碰巧选择了与我们相同的字符串).

There are two things to keep in mind if we do this: any other code can look up our data, and any other code can overwrite our data either intentionally or by accident (e.g. they just so happened to pick the same string as us).

如果我们使用符号作为键,那么其他代码就无法查找或修改数据,除非我们明确地给它们符号(例如作为参数).这样,就不会发生冲突,因为许多不同的代码都可以使用相同的字符串(如 MDN 示例中的 "foo"),但它们总是以不同的符号结束,所以有没有冲突.

If we use a symbol as a key instead, then there is no way for other code to look up or modify the data, unless we explicitly give them the symbol (e.g. as an argument). This way, clashes can't occur, since lots of different code can all use the same string (like "foo" in the MDN example) but they'll always end up with different symbols, so there's no conflict.

我们可能想要这样做的一个原因是,如果我们有一个单独的状态"对象,各种代码将对其进行修改.如果我们想保持每段代码模块化,那么重要的是它们不会通过状态间接地相互依赖:通过使用符号,我们确保不仅一段代码不会改变其他人的部分状态,但他们也不能依赖其他人的状态(即根据阅读某人的状态选择做什么).

One reason we might want to do this is if we have a single "state" object which various pieces of code will modify. If we want to keep each piece of code modular then it's important that they don't end up depending on each other indirectly via the state: by using symbols we make sure that not only can a piece of code not alter someone else's part of the state, but they also can't depend on someone else's state (i.e. choosing what to do based on reading someone's state).

这篇关于为什么 JavaScript 使用符号而不是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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