<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Xianzhong Zhu on Medium]]></title>
        <description><![CDATA[Stories by Xianzhong Zhu on Medium]]></description>
        <link>https://medium.com/@sdmyzxz5?source=rss-189627cac8da------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*T3G_IyVdtDPN0Buajv5XOg.jpeg</url>
            <title>Stories by Xianzhong Zhu on Medium</title>
            <link>https://medium.com/@sdmyzxz5?source=rss-189627cac8da------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 12:42:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sdmyzxz5/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Delve into Symbol in ES6 (1)]]></title>
            <link>https://medium.com/@sdmyzxz5/delve-into-symbol-in-es6-1-6c0c7f491c4f?source=rss-189627cac8da------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c0c7f491c4f</guid>
            <category><![CDATA[es6-in-depth]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[symbols]]></category>
            <dc:creator><![CDATA[Xianzhong Zhu]]></dc:creator>
            <pubDate>Sun, 07 Oct 2018 16:31:35 GMT</pubDate>
            <atom:updated>2018-10-08T12:01:13.880Z</atom:updated>
            <content:encoded><![CDATA[<h3>Symbol in ES6</h3><h3>Overview</h3><p>As is known, the object property names of ES5 are all strings, which is easy to cause conflicts in property names. For example, if you use an object provided by someone else, but want to add a new method (mixin mode) to the object, the name of the new method may conflict with the existing one. If there is a mechanism to ensure that the name of each property is unique, it will fundamentally prevent the conflict of attribute names. This is why ES6 introduces <strong>Symbol</strong>.</p><p>ES6 introduces a new primitive data type, Symbol, which represents a unique value. It is the seventh data type of the JavaScript language. The first six are: <strong>undefined</strong>, <strong>null</strong>, <strong>Boolean</strong>, <strong>String</strong>, <strong>Number</strong>, and <strong>Object</strong>, respectively.</p><p>The <strong>Symbol</strong> value is generated by the <strong>Symbol</strong> function. That is to say, the object’s property name can now have two types, one is the original string, and the other is the new <strong>Symbol</strong> type. Therefore, any property name that belongs to the <strong>Symbol</strong> type is unique and can be guaranteed not to conflict with other property names. Refer to the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4qQ_zPZkKJA7OCfF7Jj-QA.jpeg" /></figure><p>In the above code, the variable <strong>s</strong> is a unique value. The result of the <em>typeof</em> operator indicates that the variable <strong>s</strong> is a <strong>Symbol</strong> data type, not other types such as <strong>string</strong>.</p><blockquote><strong>NOTE</strong>: the <strong>new</strong> command cannot be used before the <strong>Symbol</strong> function, otherwise an error will be reported. This is because the generated <strong>Symbol</strong> is a primitive type of value, not an object. That is, because the <strong>Symbol</strong> value is not an object, you cannot add an property. Basically, it is a data type similar to <strong>string</strong>.</blockquote><p>There are several other points worthy to be noticed with <strong>Symbol</strong>.</p><ol><li>The <strong>Symbol</strong> function can accept a string as a parameter, indicating a description of the <strong>Symbol</strong> instance, mainly for display in the console, or when converted to a string to be easier to distinguish. Please consider the code below:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*--FQhCSDuMxH_1oHPhzLeQ.jpeg" /></figure><p>In the above code, <strong>s1</strong> and <strong>s2</strong> are two <strong>Symbol</strong> values. If no parameters are added, their outputs in the console are both Symbol(), which is not conducive to distinguish. With the parameters, it is equivalent to add a description to them, the output can be distinguished — which value is which.</p><p>2. If the parameter to <strong>Symbol</strong> is an object, the object’s toString method is called, which will convert the parameter to a string before a Symbol value is generated. Here is an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vhWmdXInE6qUKCC2IIbnPg.jpeg" /></figure><p>Note that the parameter of the <strong>Symbol</strong> function only represent the description of the current <strong>Symbol</strong> value, so the return values of the <strong>Symbol</strong> function of the same parameter are not equal. See the below code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bzY4l-qSzfMwPL5U9i-otw.jpeg" /></figure><p>In the above code, <strong>s1</strong> and <strong>s2</strong> are both the return values of the <strong>Symbol</strong> function, and also the parameters are the same, but they are not equal!</p><p>3. The <strong>Symbol</strong> value cannot be evaluated with other types of values; or else, the system will report an error. Consider the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AEgdEB3_7a36PR8qUvAcfw.jpeg" /></figure><p>However, the <strong>Symbol</strong> value can be explicitly converted to a string:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i_978KLixg7ZmM7ipXqUfw.jpeg" /></figure><p>In addition, the <strong>Symbol</strong> value can also be converted to a <strong>Boolean</strong> value, but it cannot be converted to a numeric value. Refer to code below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3ve09LPu-WX8lS7ofthc8g.jpeg" /></figure><p><em>P.S. If you enjoyed this article and want more like these, please clap ❤ and share with friends that may need it. Thanks!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c0c7f491c4f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>