TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

ES6 in Depth – Symbols

123 点作者 jansc将近 10 年前

15 条评论

nni将近 10 年前
It is my understanding that symbols were going to be a way to have private methods. This aspect of them - the reason they were going to be introduced in the first place - was dropped, and so you are left with its current limited form with a much narrower use-case.<p>From A Rossberg (he&#x27;s also the guy behind SoundScript) in a March 2014 stackoverflow post (<a href="http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;21724326&#x2F;why-bring-symbols-to-javascript&#x2F;22280202#22280202" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;21724326&#x2F;why-bring-symbol...</a>):<p>&quot;Enabling private properties ... was indeed the original motivation for introducing symbols into JavaScript. Unfortunately, however, they ended up being severely downgraded, and not private after all.<p>They are now known as unique symbols, and their only use is to avoid name clashes between properties....Whether that is strong enough a motivation to add symbols to the language is debatable.&quot;<p>and from R. Waldron as part of this response (<a href="https:&#x2F;&#x2F;esdiscuss.org&#x2F;topic&#x2F;proposal-about-private-symbol" rel="nofollow">https:&#x2F;&#x2F;esdiscuss.org&#x2F;topic&#x2F;proposal-about-private-symbol</a>) to a proposal about a private symbol (Dec 2014)<p>&quot;Ultimately it was decided that Symbol is just a symbol and that &quot;private&quot; things will be dealt with orthogonally (and at a later date).&quot;<p>YMMV
评论 #9707216 未加载
lispm将近 10 年前
&gt; Calling Symbol() creates a new symbol, a value that’s not equal to any other value.<p>Lisp:<p><pre><code> CL-USER 1 &gt; (eq (make-symbol &quot;Foo&quot;) (make-symbol &quot;Foo&quot;)) NIL </code></pre> &gt; Symbols aren’t exactly like anything else<p><pre><code> CL-USER 2 &gt; (type-of (make-symbol &quot;Foo&quot;)) SYMBOL </code></pre> &gt; Trying to concatenate a symbol with strings will result in a TypeError.<p><pre><code> CL-USER 3 &gt; (concatenate &#x27;string &quot;abc&quot; &#x27;foo &quot;def&quot;) Error: In a call to LENGTH: FOO (of type SYMBOL) is not of type SEQUENCE. </code></pre> &gt; There are three ways to obtain a symbol.<p>&gt; Call Symbol()<p><pre><code> (make-symbol &quot;FOO&quot;) </code></pre> &gt; Call Symbol.for(string)<p><pre><code> (find-symbol &quot;FOO&quot;) </code></pre> Other than that symbols in Common Lisp have a package, a value, a function and a property list. Symbols can be interned in a package or not. So-called Keyword symbols are in the package KEYWORD and have itself as the value. :I-AM-A-KEYWORD evaluates to :I-AM-A-KEYWORD.
评论 #9708212 未加载
unwind将近 10 年前
As a very green JS programmer, I found this post informative and fun.<p>I think this sentence has an editing mistake in it:<p><pre><code> It’s simply a property whose name is not a symbol rather than a string. </code></pre> I think that the &quot;not&quot; shouldn&#x27;t be there, the double negation makes the sentence fail to parse for me at least (ObCaveat: not a native speaker).
yoshuaw将近 10 年前
I wrote a lil&#x27; article about using Symbols a month ago - <a href="https:&#x2F;&#x2F;medium.com&#x2F;code-ops&#x2F;party-tricks-with-es6-symbols-ee328fdb6c4b" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;code-ops&#x2F;party-tricks-with-es6-symbols-ee...</a><p>Probably the most interesting use I&#x27;ve found for Symbols so far is to detect if an object &#x2F; function was created by a specific factory - <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;yoshuawuyts&#x2F;2bf8d5394e6f995791a0" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;yoshuawuyts&#x2F;2bf8d5394e6f995791a0</a>
BinaryIdiot将近 10 年前
Symbols are odd. Maybe it&#x27;s just me but they feel like they work &quot;funny&quot; in JavaScript.<p>As the article says you can&#x27;t implicitly convert a symbol&#x27;s description to string. Symbol is now the only native object in JavaScript that has this behavior.<p>var str = &quot;something&quot; + &quot;str&quot;; &#x2F;&#x2F; Works<p>var num = &quot;something&quot; + 5; &#x2F;&#x2F; Works<p>var func = &quot;something&quot; + function () { }; &#x2F;&#x2F; Works<p>var obj = &quot;something&quot; + { some: &quot;test&quot; }; &#x2F;&#x2F; Works<p>var bool = &quot;something&quot; + true; &#x2F;&#x2F; Works<p>var dt = &quot;something&quot; + Date.now(); &#x2F;&#x2F; Works<p>var und = &quot;something&quot; + undefined; &#x2F;&#x2F; Works<p>var nul = &quot;something&quot; + null; &#x2F;&#x2F; Works<p>var nan = &quot;something&quot; + NaN; &#x2F;&#x2F; Works<p>var sym = &quot;something&quot; + Symbol(&quot;test&quot;); &#x2F;&#x2F; Throws TypeError<p>Another thing, which is more of a style thing in my opinion, is you can actually define a property with a Symbol which just seems awkward to me. I mean sure you can use it as a property by design so why wouldn&#x27;t you be able to use defineProperty? I always felt those should be public types of properties where you can add additional logic where necessary.<p>var obj = { };<p>Object.defineProperty(obj, Symbol(&quot;MyProp&quot;), {<p><pre><code> get: function () { return 15; } </code></pre> });<p>I feel like almost the same thing with Symbol could be accomplished with a simple UUID generator. It&#x27;s a neat little thing it just feels awkward to me with how JavaScript works.
评论 #9707928 未加载
skrebbel将近 10 年前
I don&#x27;t understand why the author compares ES6 symbols to Ruby symbols; they&#x27;re <i>nothing</i> alike. It&#x27;s kind of the opposite: Ruby symbols are guaranteed to be the same object if they have the same name, ES6 symbols are guaranteed to be a <i>different</i> object, even if they have the same name (description)
评论 #9704977 未加载
meric将近 10 年前
Looks like Symbols is a type being introduced because objects, when used as a key in another object, gets turned into a string, unlike Lua tables can be used as keys of other tables, and are guaranteed to not be equal to any other value. Lua tables are built-in symbols. Javascript&#x27;s objects are not.
评论 #9705662 未加载
评论 #9705432 未加载
IshKebab将近 10 年前
I wish javascript had some kind of flag so you could get it to act sanely. All the comparison operators would work like they do in sane languages; type conversion wouldn&#x27;t be <i>quite</i> so automatic and insane, etc.
评论 #9705410 未加载
nabla9将近 10 年前
This is very limited symbol type. They might have named them keywords instead. It&#x27;s just interned string with a new type.<p>(It&#x27;s like Common Lisp symbols limited inside the keyword package)
welfare将近 10 年前
I&#x27;m not entirely convinced with the function-call syntax for declaring a Symbol.<p>Why not using something more of the lines with Ruby&#x27;s :symbol syntax?
评论 #9705634 未加载
评论 #9705211 未加载
评论 #9705221 未加载
评论 #9705082 未加载
评论 #9707944 未加载
asdf99将近 10 年前
yet another duct tape kind of feature, from people already too corrupted by js, that will be further abused and cause yet more duct tape features in ES7.<p>this is nothing but a more convoluted way of the pattern that sets a unique object as a unique value for comparison. well, it adds a label for easy debug. hooray.
评论 #9704983 未加载
评论 #9705087 未加载
agd将近 10 年前
<i>Call Symbol.for(string). This accesses a set of existing symbols called the symbol registry. Unlike the unique symbols defined by Symbol(), symbols in the symbol registry are shared. If you call Symbol.for(&quot;cat&quot;) thirty times, it will return the same symbol each time. The registry is useful when multiple web pages, or multiple modules within the same web page, need to share a symbol.</i><p>Have they added inbuilt support for globals here? People could easily add their own global symbols, why does this need to be built in?
评论 #9705557 未加载
jordanwallwork将近 10 年前
I&#x27;m not from a Ruby&#x2F;Lisp background, so I don&#x27;t know how symbols are typically used, but the explanation of why symbols are required is to prevent collisions if two libraries chose to use the same property name; so given that, what is the point of the Symbol registry? I don&#x27;t understand what the value of a symbol that is referenced by a string is, over simply using the string?
评论 #9705594 未加载
评论 #9705476 未加载
评论 #9705765 未加载
espadrine将近 10 年前
If you think that this language feature has no equivalent in your favorite language, it is similar to Ruby&#x27;s :symbol, where it is mostly used as keys to their hash tables (they&#x27;re implemented with hashes internally).<p>Common Lisp has something even closer for its macro system. When you write code that writes code, you often need to create new variable names that won&#x27;t collide or shadow anything. `(gensym)` does that: <a href="http:&#x2F;&#x2F;www.lispworks.com&#x2F;documentation&#x2F;lw50&#x2F;CLHS&#x2F;Body&#x2F;f_gensym.htm#gensym" rel="nofollow">http:&#x2F;&#x2F;www.lispworks.com&#x2F;documentation&#x2F;lw50&#x2F;CLHS&#x2F;Body&#x2F;f_gens...</a>.<p>Here&#x27;s a list of languages with support for symbols. You will see even Objective-C has it: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Symbol_%28programming%29#Support" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Symbol_%28programming%29#Suppo...</a>.<p>I&#x27;d be curious to learn how it is implemented in JS engines.
评论 #9704992 未加载
评论 #9705246 未加载
xienze将近 10 年前
&gt; Sometimes it would be awfully convenient to stash some extra data on a JavaScript object that really belongs to someone else.<p>Convenient yes, a good idea, no.<p>&gt; Other code using for-in or Object.keys() may stumble over the property you created.<p>&gt; The standard committee may decide to add an .isMoving() method to all elements. Then you’re really hosed!<p>So I dunno, maybe don&#x27;t stash properties into an object that doesn&#x27;t belong to you? It&#x27;s this sort of thing that makes me hate the culture around JavaScript. Hacks upon hacks upon hacks just to save a little effort.
评论 #9706549 未加载