<?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 Ninja JavaScript on Medium]]></title>
        <description><![CDATA[Stories by Ninja JavaScript on Medium]]></description>
        <link>https://medium.com/@NinjaJavaScript?source=rss-141ea198b26c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Ro28FAlQEl17ymsO.jpg</url>
            <title>Stories by Ninja JavaScript on Medium</title>
            <link>https://medium.com/@NinjaJavaScript?source=rss-141ea198b26c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 22:12:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@NinjaJavaScript/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[Improve your Javascript unit testing with Parameterized tests]]></title>
            <link>https://medium.com/@NinjaJavaScript/improve-your-javascript-unit-testing-with-parameterized-tests-eb809e3128d3?source=rss-141ea198b26c------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb809e3128d3</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Ninja JavaScript]]></dc:creator>
            <pubDate>Sun, 19 Mar 2017 13:16:44 GMT</pubDate>
            <atom:updated>2017-03-19T13:16:44.830Z</atom:updated>
            <content:encoded><![CDATA[<p>Parameterized tests allow a developer to run the same test over and over again using different values. This can be useful if you need to test that your function can handle a range of different inputs, including edge cases. It can be impractical to write an individual test for each input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*hNlr94cycpySfNpTBK_dJw.png" /><figcaption>Mocha unit tests</figcaption></figure><p>In this contrived example, we want to test our <em>validateName</em> function with a a range of different inputs to try and break it. Here we are using three, but what if we want to test with hundreds? We want to avoid duplicating the same test again and again.</p><h4>Parameterized tests</h4><p>Many testing frameworks such as JUnit, NUnit and MSTest offer parameterized testing as a feature and have done so for years. Unfortunately for us Javascript developers, Mocha does not support this feature out of the box. People generally seem to work around the problem by writing their own forEach loops and iteratively calling their unit tests. Though this works, it can lead to a slightly confusing, cluttered syntax which can detract from the precise purpose of the test.</p><h4>Moch param</h4><p>There is a npm package, mocha-param, that can provide a clean syntax for adding Parameterized tests to mocha. This is the example above re-written:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*DWxYGUQdUrGR8IxHEGNBLA.png" /></figure><p><em>data</em> in this example is just a list of invalid names. We can add as many as we like and we completely avoid duplication.</p><p>If you write Parameterized tests differently, or have any thoughts/reactions please let me know in the comments below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb809e3128d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript “this” keyword explained simply]]></title>
            <link>https://medium.com/@NinjaJavaScript/javascript-this-keyword-explained-simply-e90762d4945d?source=rss-141ea198b26c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e90762d4945d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[this]]></category>
            <dc:creator><![CDATA[Ninja JavaScript]]></dc:creator>
            <pubDate>Tue, 31 Jan 2017 21:11:16 GMT</pubDate>
            <atom:updated>2017-01-31T21:11:16.714Z</atom:updated>
            <content:encoded><![CDATA[<p>“this” in JavaScript is often mistakenly thought to refer to the current “scope” or simply to the enclosing object. Though the reality is more complicated, understanding the basics is actually very simple and can be broken down into just four simple scenarios. In this brief summary I will aim to give you the bulk of what you need to know.</p><h4>What is “this”</h4><p>when a function is called a new execution context is created by the JavaScript engine that exists until the function has finished executing. Every execution context contains a reference to an object, “this”.</p><p>What “this” refers to depends on where and how the function that is being executed is called. Identifying where the function is called (the “call-site”) will help you determine what “this” refers to.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/260/1*FaugJ7UHo0M_OdlJEK0egQ.png" /><figcaption>the call-site is where the function is called</figcaption></figure><p>There are only four different scenarios to remember.</p><h4>1. Default Binding</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/321/1*I8cPRM8uk8qkyo_VZuxiOQ.png" /><figcaption>Default binding</figcaption></figure><p>When calling a function in a standard way shown above, “this” will actually refer to the Global object! In the browser the Global object means the Window object. In this example the console will log out a whole heap of stuff that exists on the Global object, including the variable “a” which has also been declared on the global object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*NCXRqKrM3zwlO2q_fJQrJw.png" /><figcaption>The result of console.log(this)</figcaption></figure><p>If none of the other rules below fit with your scenario, then default binding of this is probably the binding for “this” that is being used.</p><p>One exception to remember is if strict mode is enabled. By writing “use-strict” you can prevent anything from being declared on the global object.</p><h4>2. Implicit Binding</h4><p>If the function is contained within an object then that object will will be referenced by “this”. In the example below the object foo holds a reference to bar. foo will thus be logged out when bar writes “this” to the console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*Ypt2Iv_7Bph9sWYd5hFyVA.png" /><figcaption>Implicit binding</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/1*np_YNbuS0nRqSFVMWFwuww.png" /><figcaption>The console output showing foo as the “this” context of bar</figcaption></figure><h4>3. Explicit Binding</h4><p><em>bind</em>, <em>apply</em> and <em>call</em> are all functions that can be used to explicitly set the value of “this”. The only difference between these functions is that <em>apply</em> and <em>call</em> can be used to pass parameters to the function, whereas bind is used purely to set the value of “this”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/287/1*9NW1X4Jvz2FbmtQFUY2OBw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/119/1*3NXdmqSc6SqDGFwhd7ZT8w.png" /></figure><p>In the example above we have set the “this” reference of bar explicitly in three different ways. Each time we have set “this” to be foo.</p><h4>4. New Binding</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*pXb7CAfIsYvmDzaJD291uw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/244/1*v3NeyFjSRdScEgz9YA9hQQ.png" /></figure><p>When an instance of an object is created using the <em>new</em> keyword, “this” is always set to that same instance. Therefore, in this example, an instance of foo called bar has foo referenced by “this”</p><h4>Conclusion</h4><p>This is obviously a very quick tour of the “this” keyword but hopefully it can help to illustrate the key points.</p><p>Please comment or subscribe if you would like more JavaScript concepts explained in 500 words or less :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e90762d4945d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>