Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

javascript - 将符号引入ES6的动机是什么?(What is the motivation for bringing Symbols to ES6?)

UPDATE : Recently a brilliant article from Mozilla came up.(更新 :最近有来自Mozilla精彩文章出现。)

Read it if you're curious.(好奇的话请阅读。)

As you may know they are planning to include new Symbol primitive type in ECMAScript 6 (not to mention some other crazy stuff).(如您所知,他们计划在ECMAScript 6中包括新的Symbol原语类型(更不用说其他疯狂的东西了)。)

I always thought that the :symbol notion in Ruby is needless;(我一直认为Ruby中的:symbol概念是不必要的;) we could easily use plain strings instead, like we do in JavaScript.(我们可以像使用JavaScript一样轻松地使用纯字符串。) And now they decide to complicate things in JS with that.(现在,他们决定将JS中的内容复杂化。) I don't understand the motivation.(我不明白动机。) Could someone explain to me whether we really need symbols in JavaScript?(有人可以向我解释我们是否真的需要JavaScript中的符号?)   ask by Yanis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The original motivation for introducing symbols to Javascript was to enable private properties.(将符号引入Javascript的最初动机是启用私有属性。)

Unfortunately, they ended up being severely downgraded.(不幸的是,它们最终被严重降级。) They are no longer private, since you can find them via reflection, for example, using Object.getOwnPropertySymbols or proxies.(它们不再是私有的,因为您可以通过反射找到它们,例如,使用Object.getOwnPropertySymbols或代理。) They are now known as unique symbols and their only intended use is to avoid name clashes between properties.(它们现在被称为唯一符号,它们的唯一用途是避免属性之间的名称冲突。) For example, ECMAScript itself can now introduce extension hooks via certain methods that you can put on objects (eg to define their iteration protocol) without risking them to clash with user names.(例如,ECMAScript本身现在可以通过某些方法引入扩展挂钩,您可以将这些挂钩应用于对象(例如,定义其迭代协议),而不必冒与用户名冲突的风险。) Whether that is strong enough a motivation to add symbols to the language is debatable.(是否足够强大,是否有动机在语言中添加符号尚有争议。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...