<?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 Hung HOANG on Medium]]></title>
        <description><![CDATA[Stories by Hung HOANG on Medium]]></description>
        <link>https://medium.com/@hxuanhung?source=rss-3fe816ff0ff1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*yXHI7o9Njalz1E2y.</url>
            <title>Stories by Hung HOANG on Medium</title>
            <link>https://medium.com/@hxuanhung?source=rss-3fe816ff0ff1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 15:25:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hxuanhung/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[Mastering Node.js by Q&A (Part 1)]]></title>
            <link>https://medium.com/devblogcamp/mastering-node-js-by-q-a-part-1-9f87aa2f9fef?source=rss-3fe816ff0ff1------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f87aa2f9fef</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Hung HOANG]]></dc:creator>
            <pubDate>Mon, 15 May 2017 21:47:40 GMT</pubDate>
            <atom:updated>2017-05-15T21:49:26.122Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*6Lj4amyOX_v7EcYZdNIBUQ.png" /></figure><p>Coding in Node.js is really cool but it’s sometimes challenging. That’s why understanding the basics of Node.js can help us take full advantages of the powerful language. And though my coding experience, Q&amp;As are really a good approach to learn and expand knowledge. This is my first article in the series of Mastering Node.js by Q&amp;A where I try to answer some of my favorite questions about Node.js. Hope you enjoy!</p><blockquote>What is the relationship between Node.js and V8? Can Node work without V8?</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*i9MvBlVGHqywu4EuWxdZEQ.png" /><figcaption><a href="https://arenli.com/architecture-of-node-js-internal-codebase-57cd8376b71f">Node.js Architecture (High-Level to Low-Level)</a></figcaption></figure><p>Node can not work without a Javascript engine. In fact, to run your app, your machine need to understand your codes so we need a compiler that can compile your codes written in Javascript into machine codes.</p><blockquote>And <a href="https://developers.google.com/v8/">V8</a> is Google’s open source high-performance JavaScript engine, written in C++. V8 compiles and executes JavaScript source code, handles memory allocation for objects, and garbage collects objects it no longer needs.</blockquote><p>By default Node.js uses V8, however there is project named <a href="https://github.com/nodejs/node-chakracore">Node.js on ChakraCore</a> lead by Microsoft that tries to bring Chakra, a Javascript engine developed by Microsoft and currently used for its browser Edge on Windows, to Node.</p><blockquote>When exporting the API of a Node module, why can we sometimes use exports and other times we have to use module.exports?</blockquote><p>The exports variable is available within a module&#39;s file-level scope, and is assigned the value of module.exports before the module is evaluated.</p><pre>//something like this<br>var exports = module.exports = {};</pre><p>In other words, exports is a shortcut of module.exports. When you import a module you import the reference that is pointed to by module.exports</p><pre>const Server = someFunc () {};</pre><pre>//instead of writing modules.exports.Server = Server; you can write exports.Server = Server;</pre><p>That’s why if exports is reassigned to a new object then exports and module.exports are no longer pointing to the same reference. For example, the following will give you an error.</p><pre>//Server.js<br>const Server = someFunc () {};<br>exports = Server;</pre><pre>//main.js<br>const server = require(&#39;Server.js&#39;);<br>server();</pre><p>A good practice is that when the module.exports property is being completely replaced by a new object, it is common to also reassign exports, for example:</p><pre>module.exports = exports = function Constructor() {<br>  // ... etc.<br>}</pre><blockquote>How come when you declare a global variable in any Node.js file it’s not really global to all modules?</blockquote><p>Node.js puts your module’s code into a <a href="https://nodejs.org/api/modules.html#modules_the_module_wrapper"><em>module wrapper</em></a><em>, i</em>t’s a function wrapper like this:</p><pre>(function (exports, require, module, __filename, __dirname) {<br>// Your module code actually lives in here<br>});</pre><p>By doing this, Node.js keeps top-level variables (defined with var, const or let) scoped to the module rather than the global object.</p><p>That’s it, we have gone through the first article in the series of Mastering Node.js by Q&amp;A. If you think this article is helpful please ❤ ❤ ❤. The upcoming articles of the series will arrive soon.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f87aa2f9fef" width="1" height="1" alt=""><hr><p><a href="https://medium.com/devblogcamp/mastering-node-js-by-q-a-part-1-9f87aa2f9fef">Mastering Node.js by Q&amp;A (Part 1)</a> was originally published in <a href="https://medium.com/devblogcamp">DevBlogCamp</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hacking Javascript with operators]]></title>
            <link>https://medium.com/devblogcamp/hacking-javascript-with-operators-31bdf808289?source=rss-3fe816ff0ff1------2</link>
            <guid isPermaLink="false">https://medium.com/p/31bdf808289</guid>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Hung HOANG]]></dc:creator>
            <pubDate>Fri, 07 Apr 2017 11:01:09 GMT</pubDate>
            <atom:updated>2017-04-07T16:19:00.422Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TBfzRxCkcHfTEMtXAnQjGA.png" /></figure><p>While coding in Javascript, I have found many helpful hacks that help me save tons of time, be more productive and write readable as well as elegant codes. Operators are one of them. So today I would like to share with you 5 of my favorite hacks using operators.</p><h3>1) Short-circuit evaluation</h3><p>Suppose you have the following code:</p><pre>let canBuy = true;</pre><pre>function buy() {<br> console.log(&#39;Buy&#39;);<br>}</pre><p>Now instead of coding like this:</p><pre>if (canBuy) {<br> buy(); //return &quot;Buy&quot;<br>}</pre><pre>// you can code like this<br>canBuy &amp;&amp; buy(); //return &quot;Buy&quot;</pre><h3>2) Converting to boolean using !! operator</h3><p>In Javascript you can convert expressions which return one of the following listed values to false and any other values to true by using !! operator.</p><ul><li>null; NaN; 0; empty string (&quot;&quot;); undefined</li></ul><p>This operator is really helpful when we want to check if some variable exists or if it has a valid value, to consider them as <em>true value</em>. To understand it in practice, take a look this simple example:</p><pre>function setCanPrint(quantity) {<br>  return !!quantity;<br>}</pre><pre>console.log(setCanPrint(1)); // true<br>console.log(setCanPrint(undefined)); // false</pre><h3>3) + operator to convert a string to number</h3><p>Simply return a corresponding number if it’s a valid string otherwise return NaN . Have a look on this example:</p><pre>console.log(+&#39;123&#39;); // 123</pre><pre>console.log(+&#39;12a&#39;); // NaN</pre><p>This magic will work with Date too and, in this case, it will return the timestamp number:</p><pre>console.log(+new Date()) // 1491563677307</pre><h3>4) Comma “,” operator to write readable and elegant code</h3><p>The comma , operator can help you to write readable and elegant code if you know how to use it correctly.</p><pre>hasCash ? (buy(), go()) : (saySorry(), exit());</pre><pre>//Or in another example<br>for(var a = 1, b = 10; a*b; a++, b--) {<br>   console.log(new Array(a*b).join(&#39;*&#39;));<br>}</pre><h3>5) Default values using || operator</h3><p>If the first parameter returns false the second one will be used as a default value. See this example:</p><pre>function addProduct(_product) {<br>  let product = {<br>    unsold: _product.unsold || 0,<br>    wrongLabels: _product.mistakes || 0<br>  };<br>  this.productsList.push(product);<br>}</pre><p>That’s it, I hope that these operators can help you write more readable and elegant code as well as be more productive while coding in Javascript. I will try to share with you more stuffs like this in the upcoming articles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31bdf808289" width="1" height="1" alt=""><hr><p><a href="https://medium.com/devblogcamp/hacking-javascript-with-operators-31bdf808289">Hacking Javascript with operators</a> was originally published in <a href="https://medium.com/devblogcamp">DevBlogCamp</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understand apply, call and bind methods in Javascript]]></title>
            <link>https://medium.com/devblogcamp/understand-apply-call-and-bind-methods-in-javascript-c0baaa41cb0?source=rss-3fe816ff0ff1------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0baaa41cb0</guid>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Hung HOANG]]></dc:creator>
            <pubDate>Thu, 06 Apr 2017 21:29:56 GMT</pubDate>
            <atom:updated>2017-07-28T08:49:32.897Z</atom:updated>
            <content:encoded><![CDATA[<p>Suppose we have multiples objects and each object has its own properties. Now we want to attach a function which is created elsewhere to these objects. How can we achieve this? Javascript provides apply, call and bind methods for us to achieve what we want. The usage of each method is slightly different.</p><h3>Function.prototype.apply()</h3><p>By definition, the <strong>apply()</strong> method calls a function with a given this value and arguments provided as an array (or an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections#Working_with_array-like_objects">array-like object</a>). Let’s look at a very simple example.</p><p>Let’s say we have an object named ‘obj’ and this object has one property ‘name’ like the following:</p><pre>let obj = {<br>    name: &#39;obj&#39;<br>}</pre><p>then we have a function named ‘printObj’ like this:</p><pre>function printObj (a) {<br>    console.log(&#39;print &#39;, this.name, &#39; and &#39;, a);<br>}</pre><p>Now we can use the apply() method to call this function within the context of ‘obj’. Or we can say that we are going to attach the ‘printObj’ function to the ‘obj’ object.</p><pre>printObj.apply(obj, [&#39;function&#39;]); // print  obj  and  function</pre><h3>Function.prototype.call()</h3><p>Be definition, the <strong>call()</strong> method calls a function with a given this value and arguments provided individually.</p><blockquote>The fundamental difference between call() and apply() is that <em>call()</em> accepts an <strong>argument list</strong>, while <em>apply()</em> accepts a <strong>single array of arguments</strong>.</blockquote><p>So to achieve the same result as above, we can do like this we call():</p><pre>printObj.call(obj, &#39;function&#39;); // print  obj  and  function</pre><h3>Function.prototype.bind()</h3><p>When we use call() or apply(), the ‘printObj’ function executes immediately, what if we want to execute it later, that where bind() come out.</p><p>By definition, the <strong>bind()</strong> method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.</p><p>We can see what bind() return by using console.log or console.dir</p><pre>let fn = printObj.bind(obj);<br>console.log(fn);<br>console.dir(fn);</pre><p>Now if we want to call printObj(), we just use our newly created variable and pass to it an argument.</p><pre>fn(&#39;function&#39;);</pre><p>You can test our code on <a href="https://jsfiddle.net/hxuanhung/d2xc2m90/7/">jsfiddle</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0baaa41cb0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/devblogcamp/understand-apply-call-and-bind-methods-in-javascript-c0baaa41cb0">Understand apply, call and bind methods in Javascript</a> was originally published in <a href="https://medium.com/devblogcamp">DevBlogCamp</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RxJS — Angular 2 examples: keyboard shortcuts]]></title>
            <link>https://medium.com/@hxuanhung/rxjs-angular-2-examples-keyboard-shortcuts-46b9d69393db?source=rss-3fe816ff0ff1------2</link>
            <guid isPermaLink="false">https://medium.com/p/46b9d69393db</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[rxjs]]></category>
            <dc:creator><![CDATA[Hung HOANG]]></dc:creator>
            <pubDate>Fri, 06 Jan 2017 13:45:47 GMT</pubDate>
            <atom:updated>2017-04-02T12:08:02.901Z</atom:updated>
            <content:encoded><![CDATA[<p>In the world of Tech, if there is something new come out, you can choose whether to believe and use it or not. In my case since the moment I see Reactive Programming, I believe that this will become huge. This is a series of examples that originally included in the repository of RxJs on Github. As I go through it, I rewrite it using Angular 2 and this is the first one. This example allow us to create and capture keyboard shortcuts. All of the code can be found <a href="https://github.com/hxuanhung/rxjs-angular2-examples">here</a> and the final will look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*_S0Mq16wCNdwuCDHNiaxFg.png" /></figure><p>I will put here key points that I found it’s really helpful to understand the code.</p><p><strong>Understand Subject</strong></p><p>In RX, a Subject is a proxy that acts as an observer and Observable at the same time.</p><p>Using Subject in Angular 2</p><p>While Angular 2 usually uses event handlers to manage events and RxJS typically uses Observable.fromEvent, a good practice when using Angular 2 and RxJS combined is to use Subjects and push the value through each event so that you can use it as part of your stream.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46b9d69393db" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>