<?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 Haixiang Yan on Medium]]></title>
        <description><![CDATA[Stories by Haixiang Yan on Medium]]></description>
        <link>https://medium.com/@haixiang6123?source=rss-513e8ee3b317------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*unjlZW2GlV9rtwTRnU8CsQ.jpeg</url>
            <title>Stories by Haixiang Yan on Medium</title>
            <link>https://medium.com/@haixiang6123?source=rss-513e8ee3b317------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 12:55:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@haixiang6123/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[Overwatch UI Lib]]></title>
            <link>https://medium.com/@haixiang6123/overwatch-ui-lib-1db17a9349a7?source=rss-513e8ee3b317------2</link>
            <guid isPermaLink="false">https://medium.com/p/1db17a9349a7</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[ui]]></category>
            <dc:creator><![CDATA[Haixiang Yan]]></dc:creator>
            <pubDate>Fri, 15 Mar 2019 02:36:41 GMT</pubDate>
            <atom:updated>2019-03-15T02:36:41.428Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ybsYecev7HFgRQJ-uCeI9A.jpeg" /></figure><p>I came up with an idea to build a UI library when I was playing rank on Overwatch last year. Thinking it would be a good practice, I spent 3 months in building this Library with Vue.js.</p><p>Here’s the documentation link</p><p><a href="https://haixiang6123.github.io/overwatch-ui-doc/#/">https://haixiang6123.github.io/overwatch-ui-doc/#/</a></p><p>and Github link</p><p><a href="https://github.com/Haixiang6123/overwatch-ui">https://github.com/Haixiang6123/overwatch-ui</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HppSQOefU6ieJyu-kgWKNQ.png" /></figure><p>Although its design idea may not be very compatible with Web Development, it’s still a fun experience for learning how to build components, write unit test cases and configure the whole project :)</p><p>If you like it too, please star my repo :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1db17a9349a7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue: Two-Way Binding]]></title>
            <link>https://medium.com/@haixiang6123/vue-two-way-binding-7e83e0ff26f3?source=rss-513e8ee3b317------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e83e0ff26f3</guid>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Haixiang Yan]]></dc:creator>
            <pubDate>Wed, 30 Jan 2019 07:11:29 GMT</pubDate>
            <atom:updated>2019-01-30T07:11:29.690Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ht8T-vqbqy5iG7FzNQGjFA.png" /></figure><p>Two-way binding is so popular in Vue.js. Many developers love to use v-model directive in their Web APP. Talking about how to implement this, people may say: Ah, that’s easy, what you need to do are adding a listener to input event and binding :value to &lt;input&gt; element. But what about the implementation of “binding value”?</p><p>This post is going to talk more about binding a value, and each time you change that data, it will synchronize to UI component.</p><h3>Responsive</h3><p>Responsive is a outstanding feature of Vue.js. It applies the Observer Design Pattern to let it happen. The main idea is that it traverses all properties defined in data and re-construct their getter/setter by using Object.defineProperty() . That allows Vue to trace differences of data and synchronize to Views.</p><h3>Implementation</h3><p>Here’s a simple way to do this. Let’s say we have a &lt;input&gt; element. When we change the value of it, &lt;p&gt; element will show it below.</p><p>Inside setter function of value text , we need to do the synchronization.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F3o1pqk6n1&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F3o1pqk6n1&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F3o1pqk6n1%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/87fe580672125d040a0adc9a2953dc1e/href">https://medium.com/media/87fe580672125d040a0adc9a2953dc1e/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e83e0ff26f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue: Lifecycle Hooks]]></title>
            <link>https://medium.com/@haixiang6123/vue-lifecycle-hooks-bcc3c71d9814?source=rss-513e8ee3b317------2</link>
            <guid isPermaLink="false">https://medium.com/p/bcc3c71d9814</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Haixiang Yan]]></dc:creator>
            <pubDate>Tue, 29 Jan 2019 17:44:58 GMT</pubDate>
            <atom:updated>2019-01-29T17:44:58.403Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ht8T-vqbqy5iG7FzNQGjFA.png" /></figure><p>“Can you describe the lifecycle hooks of Vue?” is a must-ask question in Front-end interview. Some people may say that’s easy.</p><pre>create -&gt; mount -&gt; update -&gt; destory</pre><p>For each period, there are 2 hooks: beforeXXX and XXXed . This post will go further and explain some thing happens for each hook.</p><h3>Hooks</h3><p>Hooks are callbacks of some specific events. You can see them as listeners. For example, when Vue dispatches created event, the created hook will be executed.</p><h3>beforeCreate and created</h3><p>After creating a Vue instance, it will invoke init() which first execute beforeCreate hook. During this time, it will do injection. For example, if you inject an eventHub , the instance will bind it to itself, then you can use this.eventHub to access it.</p><p>One thing needs to be noticed: Don’t try to modify data inside beforeCreate . It’s better to do it in mounted . I strongly recommend you to send Ajax or do any initialization inside mounted or beforeMount hook.</p><p>Let’s say you get response in created and assign value to data</p><pre>this.users = response.data.users</pre><p>Vue will try to render that data for virtual DOM, but it may still execute the render function, then it throws errors.</p><p>After created , it will check if the option el exists. If it’s not, then it will find vm.$mount(el) and execute it. Otherwise, it goes to next step directly.</p><p>Next step is to compile content of template to render function, like this:</p><pre>render (h) {   <br>  return h(&#39;div&#39;, {}, this.text) <br>}</pre><p>If you are using .vue template file to define components, vue-loader will do the compilation, and it has a higher performance.</p><h3>beforeMount and mounted</h3><p>Vue will execute the render function between these 2 life events. Therefore, the DOM element that el corresponds to is still an empty element, until virtual DOM is mounted to it.</p><p>After mounted hook, Vue instance is finally “created”</p><h3>beforeUpdate and updated</h3><p>These 2 hooks will be invoked when data is changed, and it needs to update the virtual DOM tree.</p><p>Before it updates, you can use beforeUpdate hook to access the previous DOM element or remove previous listeners.</p><p>Within the updated hook, you should avoid to update components.</p><h3>beforeDestroy and destroyed</h3><p>Usually, you are supposed to remove any listeners or binding data in beforeDestory hook. Note: even though the instance is destroyed, the DOM element that el points to still exist. If you want to remove it, then use document.querySelector(el).remove() .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bcc3c71d9814" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HTTP: Several Ways to Do Cache]]></title>
            <link>https://medium.com/@haixiang6123/http-several-ways-to-do-cache-8769ba75f10?source=rss-513e8ee3b317------2</link>
            <guid isPermaLink="false">https://medium.com/p/8769ba75f10</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[https]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Haixiang Yan]]></dc:creator>
            <pubDate>Tue, 29 Jan 2019 03:17:13 GMT</pubDate>
            <atom:updated>2019-01-29T03:17:13.333Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*akH3uy2vSLQ0ba4odSKI_w.jpeg" /></figure><p>This post is going to summary several ways to do caching for HTTP requests.</p><h3>Cache-Control</h3><p>Cache-Control is a common way to do caching. The idea is that server will add a header key-value pair for the first request:</p><pre>response.setHeader(&#39;Cache-Control&#39;, &#39;max-age=30&#39;)</pre><p><strong>This example will set 30 seconds as max age, meaning that it won’t send the same request in 30 seconds.</strong> But that’s too short of time. Usually, business company will set 1 year as the max-age :</p><pre>max-age = 1year = 365days * 24hours * 60mins * 60secs</pre><p>Take Medium as an example, they set 31536000 seconds.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*ZQ0oynER8nMJltDY6QdIAQ.png" /></figure><p>You may have the question: Would it be too long? During a year, developers may have several versions of this Web APP. Correct, the front-end team may develop several versions. For different version, the only thing they do is to change the request url, then browser won’t read the cache files but send a new quest to server. Let’s see how Medium does:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*WEuHjPdpKQ6LaxXJprtqIQ.png" /></figure><p>You see the random string eZibonAmOUoOBdtC1J53qw ? If they change the JS file, they just change the name of it. The browser will send request to get the latest version.</p><p>A more understandable way is using query params to specify the versions.</p><pre>First version<br>&lt;script src=&quot;/home.js?v=1.0&quot;&gt;&lt;script&gt;</pre><pre>Second version, it won&#39;t read cache of 1st version<br>&lt;script src=&quot;/home.js?v=2.0&quot;&gt;&lt;script&gt;</pre><h3>Expires</h3><p>Expires is similar to Cache-Control , it’s also set in the server side. <strong>The main differences for them is </strong><strong>Expires is telling that when this files is expired, while </strong><strong>Cache-Control is telling that how long this files is supposed to be expired.</strong> So, usually, the value should be a specific date.</p><pre>Expires: Wed, 21 Oct 2015 07:28:00 GMT</pre><p>However, it has a problem. The browser will compare the expire date with local computer date. If you set your computer time to 01/01/1990, then it always gets expired. So, this is not safer.</p><p>Instead, we tend to use Cache-Control to do caching.</p><h3>Etag</h3><p>Etag is little tricky. The main idea is that, for the first request, the server will generate a code for that file (Let’s say we request a /home.js) and add it to the response header.</p><pre>let fileMD5 = md5(homeJsFile)<br>respond.setHeader(&#39;ETag&#39;, fileMD5)</pre><p>Next time browser sends a request to get the same file, it will attach the ETag value. The server will generate that MD5 code (One way to generate that code) again and compare it with the attached ETag value.</p><pre>if (request.headers[&#39;if-none-match&#39;] === fileMd5) {<br>    console.log(&#39;No Response Body&#39;)<br>    response.statusCode = 304<br>}<br>else {<br>    response.setHeader(&#39;ETag&#39;, fileMd5)<br>    response.write(homeJsFile.content.toString())<br>}</pre><p>statusCode = 304 is telling the browser to read the cache. <strong>Basically, it compare the MD5 code of the old files and the new files. If they are same then read the cache, or send the new file to browser.</strong></p><p>Using this way to do caching needs the browser to send request about “Hey, did you update this file?” for each request. Then the server will tell it whether browser read the cache or not. For previous 2 ways, they ever don’t need to send the request for asking, they decide to do caching in client side.</p><p>You can find the Medium example by checking Network option in developer tool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/277/1*qgRTUmB88VmTb9e-AoJBeQ.png" /></figure><h3>Last-Modified</h3><p>For each request, server will check when the file (or other resource) is modified and compare that date with value of Last-Modified . If it realizes the file is updated for a long time, then it will send the new file back. Otherwise, it set 304 as status code to let browser read the cache.</p><p>This way is little hard to implement, so mostly people don’t often use it to do caching.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8769ba75f10" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JS: Inheritance]]></title>
            <link>https://medium.com/@haixiang6123/js-inheritance-ab684b5f85ea?source=rss-513e8ee3b317------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab684b5f85ea</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Haixiang Yan]]></dc:creator>
            <pubDate>Mon, 28 Jan 2019 18:44:41 GMT</pubDate>
            <atom:updated>2019-01-28T18:44:41.907Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6ahbWjp_g9hqhaTDSJOL1Q.png" /></figure><p>We may know very well of Inheritance in Java. However, inheritance in JavaScript is totally different to Java. This post is going to introduce some principles of JS inheritance.</p><h3>Example</h3><p>Let’s say we have 2 “class”: Human and Man. Human owns property name and a method run . Man has properties age and gender and a method fight . Writing JS in ES5:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c3d3d26469a746b9bf8d140fbc7c27d/href">https://medium.com/media/8c3d3d26469a746b9bf8d140fbc7c27d/href</a></iframe><p>You may say the gender is supposed to be placed in Human . Well, just ignore this, I believe you can arrange the properties very well in real projects. And from now on, forget Java Inheritance and do JS Inheritance!</p><h3>ES5</h3><h4>Inherit Properties</h4><p>For JS inheritance, usually we need to do properties inheritance and methods inheritance.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18cbdb8634af82cc5eecc5f8965aa954/href">https://medium.com/media/18cbdb8634af82cc5eecc5f8965aa954/href</a></iframe><p>Let’s think about how the Man to own name property. An easy way is this.name = name which is written inside of Human . So if we can change what this points to, we can reuse this code.</p><p>Finally, we have Human.call(this, name) . this will point to the new instance of Man .</p><h4>Inherit Methods</h4><p>Beside properties inheritance, we also need to inherit methods from Man.prototype . According to the prototype chain, we can have codes like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/203dc5975ffb64ac217ae25e30124ec6/href">https://medium.com/media/203dc5975ffb64ac217ae25e30124ec6/href</a></iframe><p>The key point here is Man.prototype.__proto__ = Human.prototype . When we execute code (new Man()).run() , it first looks up function run inside this instance, and then it goes to Man.prototype to check if there is a run . Oops, no run , then it goes to Human.prototype to find run via __proto__ . Bingo, it finds it and execute it.</p><h4>IE</h4><p>Easy to understand? Well, not for IE. You are not allowed to manipulate __proto__ directly, since it sees __proto__ as a private property of instance. To make Man.prototype.__proto__= Human.prototype happens, we need to do some tricky things.</p><p>From codes below</p><pre>var arr = new Array()<br>arr.__proto__ === Array.prototype // true</pre><p>We can infer that</p><pre>Man.prototype = new Human()<br>Man.prototype.__proto__ === Human.prototype // true</pre><p>It kinds of make the inheritance, but the constructor Human will be executed, which may have some side effects. So we use a temp variable to do this.</p><pre>var f = function(){}<br>f.prototype = Human.prototype<br>Man.prototype = new f()</pre><ul><li>First store Human.prototype in f.prototype</li><li>After creating instance of f and assigning it to Man.prototype , value of Man.prototype.__proto__ will be f.prototype which is same as Human.prototype</li></ul><h4>The Whole Inheritance</h4><p>Combining properties inheritance and methods inheritance, we have the final version of Object Inheritance in JS:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18214475ee3926a3c4307960ba3ca24c/href">https://medium.com/media/18214475ee3926a3c4307960ba3ca24c/href</a></iframe><h3>ES6</h3><p>ES6 provides class and extends syntax sugar. It saves us more time on implementing Object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb0c2ea6f0b81e55e9efa8be9176db42/href">https://medium.com/media/eb0c2ea6f0b81e55e9efa8be9176db42/href</a></iframe><p>But, there is always a “but”. If there is a variable a: 1 assigned to Human.prototype , this syntax is not working, because you can’t define it inside class .</p><p>A reasonable solution is like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9691cfcfaebd88da04cc000c8269990/href">https://medium.com/media/b9691cfcfaebd88da04cc000c8269990/href</a></iframe><h3>Summary</h3><ul><li>JS Inheritance includes</li></ul><ol><li>Properties Inheritance: Bind this and call Parent Object constructor function.</li><li>Methods Inheritance: child.prototype.__proto__ = Parent.prototype , using prototype chain to bunch them up.</li></ol><ul><li>ES5 implementation is difficult and tricky, but it makes us easy to understand the Inheritance Mechanism.</li><li>ES6 implementation is easy, but it can’t manipulate prototype directly. It’s not flexible enough.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab684b5f85ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JS: Deep Clone]]></title>
            <link>https://medium.com/@haixiang6123/js-deep-clone-919ff3b9e1c0?source=rss-513e8ee3b317------2</link>
            <guid isPermaLink="false">https://medium.com/p/919ff3b9e1c0</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[front-end-developer]]></category>
            <category><![CDATA[frontend-development]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Haixiang Yan]]></dc:creator>
            <pubDate>Mon, 28 Jan 2019 05:22:22 GMT</pubDate>
            <atom:updated>2019-01-28T05:22:22.168Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6ahbWjp_g9hqhaTDSJOL1Q.png" /></figure><p>This post is going to summarize 2 simple ways of Object Deep Clone.</p><h3>Using JSON</h3><ol><li>Transform the Object to a string with JSON.stringify() .</li><li>Then transform it back to Object back.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f0c6965729fca3f1378bffc5085a989/href">https://medium.com/media/4f0c6965729fca3f1378bffc5085a989/href</a></iframe><h4>Pros</h4><ol><li>Simple and easy</li></ol><h4>Cons</h4><ol><li>Not support for Function , Array , RegExp , Date and etc.</li></ol><h3>Recursion</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42d8e0a6c86bb3d177c88add0b07db69/href">https://medium.com/media/42d8e0a6c86bb3d177c88add0b07db69/href</a></iframe><h4>Pros</h4><ol><li>Support for Object, like Object , Array and Function .</li></ol><h4>Cons</h4><ol><li>May have performance problem, if the object nest a variable in very deep level.</li></ol><p>2. Needs to check many types (Codes above just check 3).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=919ff3b9e1c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JS: Closure]]></title>
            <link>https://medium.com/@haixiang6123/js-closure-767573410c3c?source=rss-513e8ee3b317------2</link>
            <guid isPermaLink="false">https://medium.com/p/767573410c3c</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[frontend]]></category>
            <dc:creator><![CDATA[Haixiang Yan]]></dc:creator>
            <pubDate>Mon, 28 Jan 2019 03:35:52 GMT</pubDate>
            <atom:updated>2019-01-28T03:35:52.861Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6ahbWjp_g9hqhaTDSJOL1Q.png" /></figure><p>“Closure” is an important concept in JS. It’s not a high-level skill but a side effect of JS.</p><h3>Quick Look</h3><p>Let’s take a quick look of Closure.</p><pre>!function(){<br>  var counter= 5<br><br>  window.add= function(){<br>    counter += 1<br>  }<br><br>  window.minus = function(){<br>    counter -= 1<br>  }<br>}()</pre><p>Inside the IIFE ( (Immediately Invoked Function Expression), there is a local variable counter and 2 functions. Outside of the IIFE, we can use these 2 functions to control and access value of counter .</p><pre>window.add()</pre><pre>window.minus()</pre><p>That’s a closure which is using function to access local variables.</p><h3>Why We Need This</h3><p>In ES5, the way we define a local variable inside the block scope is using IIFE. Then it won’t affect other parts of our codes. But there is a problem: what if some blocks need that local variable?</p><p>To solve this, JS developers come up with an idea: Exposing an API (A way) to access and control it (Just like getter or setter method in Java). Then we have a new “word” — Closure: function (API that exposed by that scope) manipulate local variable of that scope.</p><p>Here’s another example:</p><pre>function fn(){<br>  var name = &quot;Jack&quot;<br><br>  return function(newName){<br>    name = newName<br>  }<br>}</pre><pre>var setName = fn() // setName is the API<br>setName(&quot;Bob&quot;)</pre><p>If you don’t expose some functions (API), you can’t use the local variables.</p><h3>Summary</h3><ol><li>Closure is mainly for hiding a variable and accessing it indirectly.</li><li>It’s just a concept. You may use it every time without knowing this name, so you don’t need to know it.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=767573410c3c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JS: Using Promise to send Ajax]]></title>
            <link>https://medium.com/@haixiang6123/js-using-promise-to-send-ajax-e03a48c8dc8?source=rss-513e8ee3b317------2</link>
            <guid isPermaLink="false">https://medium.com/p/e03a48c8dc8</guid>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[frontend]]></category>
            <dc:creator><![CDATA[Haixiang Yan]]></dc:creator>
            <pubDate>Mon, 28 Jan 2019 01:07:39 GMT</pubDate>
            <atom:updated>2019-01-28T01:07:39.798Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Lv_ke7T9sIfh7WKM1-MhA.png" /></figure><p>If you use Vue.js before, you may very familiar with axios which is an awesome library to send Ajax. This post is going to use Promise to implement the simple part of it.</p><h3>Using Vanilla JS to Send Ajax</h3><p>There are mainly 4 steps to send Ajax with JS:</p><ol><li>Create XMLHttpRequest object.</li><li>Invoke open function with method and url parameter.</li><li>Set onreadystatechange callback.</li><li>Invoke send to send request with given data.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c0f63f5aa6d29c6f8bbb4e7bea3b58b/href">https://medium.com/media/2c0f63f5aa6d29c6f8bbb4e7bea3b58b/href</a></iframe><p>It seems perfect, but there is a problem when using Promise. I’ll talk about it latter.</p><h3>Promise</h3><p>Promise constructor function accept a function that will be executed when creating a Promise object. That function has resolve and reject as its parameters. Also, Promise will follow these 2 rules.</p><ol><li>When it changing state to fulfilled , it will invoke resolve function.</li><li>When it changing state to rejected , it will invoke reject function.</li></ol><p>Then we may come up with the codes like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59d6a8e0dc75488502c191131d4871ea/href">https://medium.com/media/59d6a8e0dc75488502c191131d4871ea/href</a></iframe><p>Note: functions resolve and reject won’t be invoked until Promise state is changed.</p><h3>The Problem</h3><p>Like I said there will be a problem when using Promise . When sending a Http request, it will invoke the onreadystatechange callback for several times.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/117/1*HxJrgq1FhSOVfE7PLlm0Cw.png" /></figure><p>When readyState === 2 , the codes will go to line 8 and invoke function reject . After calling reject , this Promise will change its state to rejected , and it’s done. It’s not going to change its state anymore! The result will be like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*lJQbpxmbm0NZOBm-ryrzmQ.png" /></figure><p>The correct way is like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbb3a02a209e52f5645fb765e7b73271/href">https://medium.com/media/dbb3a02a209e52f5645fb765e7b73271/href</a></iframe><h3>Wrap It</h3><p>After solving this problem, we’d better to wrap these codes into a function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57d38c38445d15441e662419e3cb2861/href">https://medium.com/media/57d38c38445d15441e662419e3cb2861/href</a></iframe><p>Other developers can use our library with then() .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/337e3fe62d20f005fbd4610fd0655dc8/href">https://medium.com/media/337e3fe62d20f005fbd4610fd0655dc8/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e03a48c8dc8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Finally We Can View Highlight Codes in Medium]]></title>
            <link>https://medium.com/@haixiang6123/finally-we-can-view-highlight-codes-in-medium-94673365ac0?source=rss-513e8ee3b317------2</link>
            <guid isPermaLink="false">https://medium.com/p/94673365ac0</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[developer]]></category>
            <dc:creator><![CDATA[Haixiang Yan]]></dc:creator>
            <pubDate>Tue, 22 Jan 2019 18:27:07 GMT</pubDate>
            <atom:updated>2019-01-22T20:38:00.072Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CK5gSIaeULS1fsx-HKvwlA.png" /></figure><p>Honestly, I don’t know why Medium doesn’t highlight our codes when adding a code block. So I built a Chrome Extension to highlight codes before you read articles.</p><h3>Screenshot</h3><p>Let’s take a quick look.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RRujuZ1sZtYrxT6ndzCPpQ.png" /></figure><h3>Install &amp; Download</h3><p>You can install from Chrome Extension Store.</p><p><a href="https://chrome.google.com/webstore/detail/code-block-beautifier/gpcjjddhdnilcbddlonlfgdbejfboonn">Code Block Beautifier</a></p><h3>Usage</h3><p>The popup setting panel looks like a editor XD. You can change your preference here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TW9zwCPP5GJ3CncP_2cdig.png" /></figure><h3>Features</h3><ul><li>⚒ Beautify any code blocks wrapping in &lt;pre/&gt; automatically.</li><li>😄 Beautify code blocks according to your languages preference.</li><li>😎 Auto detect what languages that the author defines</li><li>🎁 Support to highlight codes in <a href="https://medium.com/">Medium</a>, <a href="https://stackoverflow.com/">StackOverflow</a> and etc.</li><li>🎉 More than 80 themes are available. Can switch to any language highlight solutions.</li><li>🎊 More than 20 language highlight themes are available. Can switch to any highlight themes you like.</li></ul><h3>Github</h3><p>Get more information from Github.</p><p><a href="https://github.com/Haixiang6123/codeblock-beautifier">Haixiang6123/codeblock-beautifier</a></p><p>Or watch the introduction video on Youtube.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FXdkuZrC1PJQ%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXdkuZrC1PJQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FXdkuZrC1PJQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/302265f2b750a80f7cb7e58a641340c5/href">https://medium.com/media/302265f2b750a80f7cb7e58a641340c5/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94673365ac0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue: EventHub in Vue]]></title>
            <link>https://medium.com/@haixiang6123/vue-eventhub-in-vue-64f5b913db8d?source=rss-513e8ee3b317------2</link>
            <guid isPermaLink="false">https://medium.com/p/64f5b913db8d</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Haixiang Yan]]></dc:creator>
            <pubDate>Mon, 21 Jan 2019 19:27:31 GMT</pubDate>
            <atom:updated>2019-01-21T19:27:31.113Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*0_zl1YRcPy0ymOpgJ0dQKA.jpeg" /></figure><p>EventHub is a very important and useful tool for Irrelevant Components Communication.</p><h3>EventHub</h3><p>Think about passing data from the top level component to low level component. It’s very annoying, if we still use parent-to-child approach to do it</p><p>EventHub can handle this easily. First, add a listener (callback) on the component that needs data. For each time the top level component passing data, it broadcasts an event and invoke the listener (callback). We can find that there are mainly 2 functions of EventHub: Listening and Broadcasting. Well, Vue just has these features. Let’s describe them with Vue:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f83361bf8be552049a21f729bfcc7e2/href">https://medium.com/media/3f83361bf8be552049a21f729bfcc7e2/href</a></iframe><p>Basically, we use the way of listening and triggering an event, provided by Vue.</p><h3>Dependency Injection</h3><p>The first time I heard “Dependency Inject”, I thought it was some kinds of high level usages, but actually it just make variables “globally” to specific components.</p><p>Let’s go back to EventHub. Apparently, it should be global for any components. Are we going to put it in Vue.prototype and use this.eventHub to use it? Vue gives us a better way to do this.</p><p>In the top level component, it defines EventHub in data and uses provide to expose this EventHub variable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5c6ebafcde90e056ebc9a717f986518/href">https://medium.com/media/e5c6ebafcde90e056ebc9a717f986518/href</a></iframe><p>For those components that need msg , they should use inject to inject EventHub and add listeners inside created() .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95791c403ffc741072e42326e99452dd/href">https://medium.com/media/95791c403ffc741072e42326e99452dd/href</a></iframe><p>The final result is Child component will always get the latest msg after changing it inside App component.</p><h3>Codes</h3><p>I wrote an example in CodeSandBox. Check it below.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fvqrq934mrl&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fvqrq934mrl&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fvqrq934mrl%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f13ec14b619c515508b2d173b4275b1/href">https://medium.com/media/8f13ec14b619c515508b2d173b4275b1/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64f5b913db8d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>