<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dima Kuzmich</title>
    <description>The latest articles on DEV Community by Dima Kuzmich (@forforeach).</description>
    <link>https://dev.to/forforeach</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F194723%2F1d6744aa-ab0c-4f8d-aa99-2641d646d100.png</url>
      <title>DEV Community: Dima Kuzmich</title>
      <link>https://dev.to/forforeach</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/forforeach"/>
    <language>en</language>
    <item>
      <title>Test Angular2 now or how to set up Angular2 unit tests</title>
      <dc:creator>Dima Kuzmich</dc:creator>
      <pubDate>Fri, 21 Aug 2015 12:57:43 +0000</pubDate>
      <link>https://dev.to/forforeach/test-angular2-now-or-how-to-set-up-angular2-unit-tests-1ek8</link>
      <guid>https://dev.to/forforeach/test-angular2-now-or-how-to-set-up-angular2-unit-tests-1ek8</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZJ_Xb8MS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/455xvwgo96tczpkvvn8l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZJ_Xb8MS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/455xvwgo96tczpkvvn8l.png" alt="angular"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Currently, I’m working on a project and I decided to build it with Angular2. It was a brave decision. It’s really not as simple as it looks when you try to build something with alpha versions of new technology. Loads of changes that part of them are breaking, difficulty in lack of documentation. Nevertheless, it’s incredibly exciting to see how Angular is growing up, to observe a collaboration of the team, and to feel a part of this interesting process.&lt;/p&gt;

&lt;p&gt;One of the tasks that weren’t trivial for me is to set up the unit tests for my project. That’s the reason for my decision to describe this process. So let’s begin:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;TypeScript&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;As &lt;a href="http://blogs.msdn.com/b/typescript/archive/2015/03/05/angular-2-0-built-on-typescript.aspx"&gt;Microsoft published&lt;/a&gt; nearly half-a-year ago, Angular2 is written in TypeScript. At the beginning that was a piece of shocking news for me. But later on, I’ve understood this step. I checked out a new version of TypeScript and was surprised to discover that it’s pretty close to ES6, erhh… JavaScript 2015?!.. Mmm… never mind, the latest JavaScript version :-) It also brings additional features, such as optional types, annotations, and more. These facts were convincing enough to try to write my project with TypeScript. I think that it is pretty clear, that if a code is written in TypeScript, tests should be written in TypeScript as well. If the tests are written in TypeScript, I should transpile them into ES5. And so I did with two tasks of &lt;a href="http://gulpjs.com/"&gt;Gulp&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CqTSFbGS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1pp9cynj62feytfmd5ut.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CqTSFbGS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1pp9cynj62feytfmd5ut.png" alt="gulp typescript tests build"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Source code is &lt;a href="https://github.com/forforeach/Angular2-Unit-Tests/blob/master/gulpfile.js"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Karma&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;a href="http://karma-runner.github.io/0.13/index.html"&gt;Karma&lt;/a&gt;is a test runner that was developed by the Angular team during the development of &lt;a href="https://angularjs.org/"&gt;Angular.js&lt;/a&gt;. Among its main features is the ability to run tests fast and on real browsers. Also, it’s agnostic to the testing framework so that you can use whatever framework you’d like. I chose a &lt;a href="http://jasmine.github.io/"&gt;Jasmine&lt;/a&gt;, but you can easily switch it to another one. So I installed Karma and Jasmine on my environment and started to set up my first test. here is an interesting part of the file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ij17_HLJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9kjzok25l2li6i2673fp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ij17_HLJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9kjzok25l2li6i2673fp.png" alt="karma config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Source code is &lt;a href="https://github.com/forforeach/Angular2-Unit-Tests/blob/master/karma.conf.js"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;First of all, we need to add traceur-runtime.js. These are basically polyfills that allow us to use ES6 features in the browser despite their not being implemented yet.&lt;/p&gt;

&lt;p&gt;Then we add our source files. The files should already be transpiled into ES5 code and should be only served by Karma and not actually be included with a script tag. That’s because we will import them into the tests using &lt;a href="https://github.com/systemjs/systemjs"&gt;System.js&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And here they are es6 module loader and system.js files. System.js is a universal dynamic module loader that loads ES6 modules, AMD, CommonJS, and global scripts in the browser and NodeJS. Later on, we will see how to configure System.js to work with our tests properly.&lt;/p&gt;

&lt;p&gt;We should definitely add an angular2 file :-) and then our test files. They also should be served and not included.&lt;/p&gt;

&lt;p&gt;Surely, you noticed this &lt;strong&gt;test-main.js&lt;/strong&gt; file that I included but didn’t explain. This file is the highlight of this post! So, Ladies and Gentlemen please welcome:&lt;/p&gt;

&lt;h4&gt;
  
  
  The System.js hook
&lt;/h4&gt;

&lt;p&gt;As I said before, in order to support modules’ imports we use System.js. Currently, Karma does not support this feature, so that we need to implement some workaround that will be able to import all needed modules and only then to run our tests. Here is what we need to do:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x-UNSyAm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rvzl3du85q9yg3jgpl05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x-UNSyAm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rvzl3du85q9yg3jgpl05.png" alt="karma systemjs hook"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First of all, you should stop karma from executing. This is made by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\_\_karma\_\_.loaded = function () { };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, we should configure our system.js with appropriate files by setting baseUrl and paths properties of System global object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;System.baseURL = ‘/base/scripts/’;
System.paths = {
 ‘test/\*’: ‘/base/scripts/test/\*.js’,
 ‘build/\*’: ‘/base/scripts/build/\*.js’,
 ‘angular2/\*’: ‘angular2/\*’,
 ‘rx’: ‘rx’
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then we take all files that served by Karma, filter only test files, normalize file names to module names and import them one-by-one using System.import method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One important thing to notice&lt;/strong&gt;: each test file has to export the main function that includes whole tests related to a module. When the module is imported, we run its main method in order to expose the tests. Here is an example of a test module:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MBcFb-CX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zyk8z9gt0oqjl6m2gbi8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MBcFb-CX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zyk8z9gt0oqjl6m2gbi8.png" alt="test example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lastly, when entire tests were imported and exposed we run the karma engine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\_\_karma\_\_.start(); // after all tests were exposed
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And voilà, our Angular2 test environment is ready!&lt;/p&gt;

&lt;p&gt;You can find the &lt;a href="https://github.com/forforeach/Angular2-Unit-Tests"&gt;source code on Github&lt;/a&gt;. To set up the environment, just clone the repository and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Also, you are welcome to check out an &lt;a href="https://github.com/EladRK/angular-starter"&gt;angular-starter&lt;/a&gt; that my college Elad and I developed. It’s really simple and uses gulp for a build workflow.&lt;/p&gt;

&lt;p&gt;I’ve based my test environment on an angular test environment. You can find their tests-main.js &lt;a href="https://github.com/angular/angular/blob/master/test-main.js"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One more thing, if you liked this post, please share it. For any question, you can find &lt;a href="https://twitter.com/forforeach"&gt;@me on Twitter&lt;/a&gt;&lt;/p&gt;




</description>
      <category>angular2</category>
      <category>frontend</category>
      <category>angular</category>
      <category>webdeveloper</category>
    </item>
    <item>
      <title>Prevent Gulp from stopping watching of files after exception</title>
      <dc:creator>Dima Kuzmich</dc:creator>
      <pubDate>Thu, 26 Feb 2015 10:43:39 +0000</pubDate>
      <link>https://dev.to/forforeach/prevent-gulp-from-stopping-watching-of-files-after-exception-44j6</link>
      <guid>https://dev.to/forforeach/prevent-gulp-from-stopping-watching-of-files-after-exception-44j6</guid>
      <description>&lt;p&gt;Recently I developed some small project and decided to try and build automated workflow with &lt;a href="http://gulpjs.com/"&gt;Gulp&lt;/a&gt;. It was very fun and easy. I really liked its features and node-like syntax.&lt;/p&gt;

&lt;p&gt;My project included &lt;a href="http://facebook.github.io/react/"&gt;React.js&lt;/a&gt; and I used their &lt;a href="http://facebook.github.io/react/docs/jsx-in-depth.html"&gt;JSX&lt;/a&gt; technology. I wanted to debug my code, thus I needed to compile JSX into vanilla JavaScript. Here is where Gulp came to my rescue. You can easily install gulp-react and it does the magic.&lt;/p&gt;

&lt;p&gt;After building my tasks and integrating a compiling I wanted to run my tasks constantly. That’s not a problem when you use Gulp. Just use &lt;strong&gt;gulp.watch()&lt;/strong&gt; and there you are. But after I’ve started to change code, I’ve understood, that my compiled file isn’t updating. When I took a look into a console, I noticed the exception after which my watching task stopped:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---0R0rnvl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gtvvmhbztv3jan9ddzxr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---0R0rnvl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gtvvmhbztv3jan9ddzxr.png" alt="exception"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The fix for this problem was another gulp package that called &lt;a href="https://www.npmjs.com/package/gulp-plumber"&gt;gulp-plumber&lt;/a&gt;. This plugin prevents pipe breaking caused by exceptions. Generally saying, it overrides pipe method of gulp and removes &lt;strong&gt;onerror&lt;/strong&gt; handler which un-pipes streams on an error by default.&lt;/p&gt;

&lt;p&gt;To sum it up, it was a really good experience using Gulp and its plugins.&lt;/p&gt;




</description>
      <category>frontend</category>
      <category>gulp</category>
      <category>web</category>
      <category>webdeveloper</category>
    </item>
    <item>
      <title>Binding event to all elements except specific element and all its contents.</title>
      <dc:creator>Dima Kuzmich</dc:creator>
      <pubDate>Wed, 28 May 2014 09:29:23 +0000</pubDate>
      <link>https://dev.to/forforeach/binding-event-to-all-elements-except-specific-element-and-all-its-contents-3c29</link>
      <guid>https://dev.to/forforeach/binding-event-to-all-elements-except-specific-element-and-all-its-contents-3c29</guid>
      <description>&lt;p&gt;Recently I’ve encountered a challenge to close navigation menu on every touch or click, except clicks and touches in the menu area. I’ve tried many ways to resolve it, and at last, I’ve found the optimal solution for me. The solution is in two steps and relies on event bubbling.&lt;/p&gt;

&lt;p&gt;Firstly, I’ve attached a click/touch event on the &lt;/p&gt; that on every click/touch closes the menu.&lt;br&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function clickOnBody(event){
 // you can close here the menu
 alert(‘clicked everywhere except the menu area’);
}
document.body.addEventListener(‘mousedown’, clickOnBody, false);
document.body.addEventListener(‘touchstart’, clickOnBody, false);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Secondly, I’ve attached another click/touch event on a specific element, and inside this event, I’ll prevent event propagation (bubbling), so that every click/touch inside element area will stop on the parent element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function clickOnMenu(event){
 event.stopPropagation();
}
var menu = document.getElementById(‘menu’);
menu.addEventListener(‘mousedown’, clickOnMenu, false);
menu.addEventListener(‘touchstart’, clickOnMenu, false);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can check the solution in &lt;a href="http://jsfiddle.net/dima_k/d9JRu/1/"&gt;jsFiddle&lt;/a&gt;&lt;/p&gt;




</description>
      <category>frontend</category>
      <category>javascripteventhan</category>
      <category>javascript</category>
      <category>web</category>
    </item>
    <item>
      <title>Memory leaks in SPA built with Knockout.js</title>
      <dc:creator>Dima Kuzmich</dc:creator>
      <pubDate>Mon, 28 Apr 2014 12:08:03 +0000</pubDate>
      <link>https://dev.to/forforeach/memory-leaks-in-spa-built-with-knockout-js-59ej</link>
      <guid>https://dev.to/forforeach/memory-leaks-in-spa-built-with-knockout-js-59ej</guid>
      <description>&lt;h3&gt;
  
  
  Memory leaks in SPA built with Knockout.js
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The Problem:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we build large SPA applications with MV* frameworks that have data bindings, such as Knockout.js, we actually create a connection between our JS models and DOM elements. Those connections may be done in a number of ways, but the bottom line is that DOM elements may have references to our models. In Knockout, for example, every element that has bindings has relation to closure object that stores reference to binding context. This binding context has a pointer to our model. Now, with this in mind, we can see the problem that will come. If we simply remove the DOM element that has bindings, a closure from above will still hold a pointer to our model and as a result, will be never released from memory. This problem is especially critical in SPA applications as to change page we simply swap inner HTML of some container and never do full reload of the page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Detection:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To detect an above problem in SPA with Knockout we don’t need to do much. If we bind our models and never unbind them, we almost certainly have a leak. However, I’ll show a way to ensure it with Chrome DevTools.&lt;/p&gt;

&lt;p&gt;We need to run our application and to open a DevTools (F12 for Windows users). I created a simple application with Knockout and Sammy.js for routing. &lt;a href="https://docs.google.com/uc?id=0B3tmSeYWpEi4eWxSRERuenVINGc&amp;amp;export=download"&gt;Download it here&lt;/a&gt;. After we run an application we need to take a heap snapshot. Press ‘Profiles’ tab in DevTools, check ‘Take Heap Snapshot’ and press ‘Take Snapshot’&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F_YBYyl1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o0lozambh4nwxnbv3mh7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F_YBYyl1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o0lozambh4nwxnbv3mh7.png" alt="Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that change between pages 1 and 2 for 10 times and take another snapshot. Now, when we have these two snapshots, we can compare them. We simply switch the mode of snapshot preview to ‘Comparison’&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ORN0G3Lb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wbsqfwvp9tamtyyp2grx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ORN0G3Lb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wbsqfwvp9tamtyyp2grx.png" alt="Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While in comparison mode, we can sort our results by the ‘# Delta’ field. This field says how many objects from a particular type are living now in memory. You can also check how many new objects were created and how many objects were released by looking at ‘# New’ and ‘# Deleted’ fields respectively. What we are looking for now are the objects that created and never released. In my test application is very simple. We know that we visited each page 5 times, so I will look for objects that have 5 in the ‘Delta’ field.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ei3Sbe2b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rla19ik6kvprb9b5gjei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ei3Sbe2b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rla19ik6kvprb9b5gjei.png" alt="Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, neither VM1 nor VM2 was released during our session.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution to this problem is very simple. As I said before, we only need to release bindingContext object of the DOM node that we want to remove. Knockout provides us with a number of methods that could be useful in this situation. We can use a ko.cleanNode(element) method before we remove an HTML or we can use ko.removeNode(element), that cleans Node and also removes it. No matter which way we are choosing to use, we need always remember  &lt;strong&gt;— If we created bindings, we must unbind them!&lt;/strong&gt;&lt;/p&gt;




</description>
      <category>frontend</category>
      <category>sap</category>
      <category>knockoutjs</category>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript Global Object, Namespaces and Object constructors</title>
      <dc:creator>Dima Kuzmich</dc:creator>
      <pubDate>Sat, 09 Nov 2013 14:47:15 +0000</pubDate>
      <link>https://dev.to/forforeach/javascript-global-object-namespaces-and-object-constructors-431i</link>
      <guid>https://dev.to/forforeach/javascript-global-object-namespaces-and-object-constructors-431i</guid>
      <description>&lt;h4&gt;
  
  
  Global Object
&lt;/h4&gt;

&lt;p&gt;A global object is an object that created once the application is running. We can’t recreate it or affect his creation in any way. After a creation Global Object uses as a global scope of the application. Actually, in JavaScript, we haven’t global variables and functions. All variables and functions that defined globally become properties of the Global Object. So, all variables in JavaScript are placed under the Global Object. ECMA-262 does not indicate a way to access the Global object directly, but we can implement the access to him by using this.&lt;/p&gt;

&lt;p&gt;The example below shows you how to do it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var globalObject = (function(){
 return this;
})();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="http://jsfiddle.net/dima_k/2Apbb/2/"&gt;Check it on jsFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In web applications the browser implementation of ECMA-262 has an object called window that uses as a delegate to a Global Object, so all the way we can access him very simply:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var globalObject = window;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above examples, we created variables that hold a reference to the Global Object. As discussed previously, all global variables become properties of Global Object, so we simply created properties on Global Object that refer to themselves.&lt;/p&gt;

&lt;p&gt;Because all new variables and functions are placed under the Global Object, there is a very high chance of name collision. As we know, JavaScript is loosely typed language and as such he is not preserving a type for created variables. So, all variables can overhead other variables just by using the same name. This is a name collision that we talked about before.&lt;/p&gt;

&lt;p&gt;To prevent a name collision we can use a number of techniques such as using namespaces, using modules, and more.&lt;/p&gt;

&lt;h4&gt;
  
  
  Namespaces
&lt;/h4&gt;

&lt;p&gt;Namespaces are objects that created under Global Object and hold variables and functions. Think about it like a tree. A tree has a root (Global object) and branches. Every branch is a new namespace.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jRLu_Z5j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q6pyi9z2qpt9iai9foqx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jRLu_Z5j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q6pyi9z2qpt9iai9foqx.png" alt="Variables-tree"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To implement it in JavaScript we use next code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myNamespace = myNamespace || {};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The left side of the example above simply declares a variable for our namespace. Right side a little bit more complicated. First of all, we try to get a namespace with the same name, if declared somewhere in the application. This technique prevents the recreation of the same namespace and not allow us to overhead his logic. Another pro of this technique is that we can use the same namespace in several files. Anyway, if a namespace was not declared before in application, we simply assign the variable with the empty object and thereby create a new namespace.&lt;/p&gt;

&lt;h4&gt;
  
  
  Object constructors
&lt;/h4&gt;

&lt;p&gt;As we know in JavaScript we can create a new object in two ways. First way is to use the &lt;em&gt;new&lt;/em&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var rectangle = new Object();
rectangle.a = 10;
rectangle.b = 5;
rectangle.getPerimeter = function() {
 return 2\*(this.a + this.b);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Second way is to use object literal that is commonly used way. The big pro of this way is that you can encapsulate a creation of an object. The previous example can be rewritten as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var rectangle = {
 a: 10,
 b: 5,
 getPerimeter: function() {
 return 2\*(this.a + this.b);
 }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Two ways above show us how to create a simple JavaScript object. It’s fine, but what if I want to create number of rectangles in my application? Do I need to duplicate that code for every rectangle instance? The answer, of course, is no. We can use a constructors. Two previous examples can be rewritten with constructor as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Rectangle(a, b) {
 this.a = a;
 this.b = b;
 this.getPerimeter = function() {
 return 2\*(this.a + this.b);
 };
}
var rectangle = new Rectangle(10,5);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Constructors in ECMAScript are used to create specific types of objects. Constructors provide us with the ability to create new object instances in a simple way. More of that, every instance created with a constructor can be identified which type is he.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Rectangle(a, b) {
 this.a = a;
 this.b = b;
 this.getPerimeter = function() {
 return 2\*(this.a + this.b);
 };
}
var rectangle = new Rectangle(10,5);
alert(rectangle.constructor == Rectangle); // alerts true
alert(rectangle instanceof Rectangle); //alerts true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="http://jsfiddle.net/dima_k/z97cn/2/"&gt;Try it in jsFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The example above shows that we can identify the type of object in two ways. By checking his constructor property and by using &lt;em&gt;instanceof&lt;/em&gt; operator. During object creation, the constructor property is automatically assigned with a reference to the constructor function. But the constructor is not placed on the object itself. It’s placed on a prototype of the object. We can check the object prototype by inspecting an object by Dev Tools or by accessing a __proto__ property of an object in Chrome, Safari, and Firefox.&lt;/p&gt;

&lt;p&gt;So until now, we can understand 3 important things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;All variables and functions are placed on the Global Object&lt;/li&gt;
&lt;li&gt;To prevent name collision we can use a namespaces&lt;/li&gt;
&lt;li&gt;We need to use a constructor to create specific types of objects&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But when we look at constructors we can understand that it is just regular JavaScript function and as such, when placed on global scope, can be easily overwritten by another function with same name. So we will want to place it under namespace as well. It can look like follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var namespace = namespace || {};
namespace.Rectangle = function(a, b) {
 this.a = a;
 this.b = b;
 this.getPerimeter = function() {
 return 2\*(this.a + this.b);
 };
}
var rectangle = new namespace.Rectangle(10,5);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It’s good, but when we will expect a constructor of the rectangle, we will see that there is an anonymous function so the prototype will be of the regular Object type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--leqE8rbr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jypwx6bghnd0n1cx8kyy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--leqE8rbr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jypwx6bghnd0n1cx8kyy.png" alt="proto"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we want, is to be able with a namespace to look at prototype and to identify that this object is instance of Rectangle. I still can use &lt;em&gt;instanceof&lt;/em&gt; operator and it’s smart enough to say me that is a Rectangle, but as a developer I will like to see it in my DevTools as well and to know that object prototype is Rectangle. To achieve that, we can use a next trick:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var namespace = namespace || {};
namespace.Rectangle = function &amp;lt;strong&amp;gt;Rectangle&amp;lt;/strong&amp;gt;(a, b) {
 this.a = a;
 this.b = b;
 this.getPerimeter = function() {
 return 2\*(this.a + this.b);
 };
}
var rectangle = new namespace.Rectangle(10,5);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What I did is I simply gave the name to the constructor function. Now we can see that our object constructor is under namespace and we still can see that object prototype is Rectangle:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HlcFmqbs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hat21dlygmp7us7kj20v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HlcFmqbs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hat21dlygmp7us7kj20v.png" alt="proto2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using this way take care of IE &amp;lt;= 8.&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;




</description>
      <category>namespaces</category>
      <category>javascript</category>
      <category>constructor</category>
      <category>javascriptarchitect</category>
    </item>
    <item>
      <title>Prevent scrolling of parent element with JavaScript</title>
      <dc:creator>Dima Kuzmich</dc:creator>
      <pubDate>Tue, 16 Jul 2013 13:16:30 +0000</pubDate>
      <link>https://dev.to/forforeach/prevent-scrolling-of-parent-element-with-javascript-41hi</link>
      <guid>https://dev.to/forforeach/prevent-scrolling-of-parent-element-with-javascript-41hi</guid>
      <description>&lt;p&gt;In my current project, I was asked to avoid scrolling of the parent element, when reaching the end of the scroll of the inner element.&lt;/p&gt;

&lt;p&gt;It sounds like a very simple task. Not when you want to implement it cross-browser. So, I searched a little and found&lt;a href="https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th/chapter-17/handling-mousewheel-events"&gt;next example&lt;/a&gt; from the book: “JavaScript: The Definitive Guide”. By the way, it’s a very good book to start with when you want to learn JavaScript basics.&lt;/p&gt;

&lt;p&gt;So, based on this example&lt;a href="http://jsfiddle.net/dima_k/5mPkB/1/"&gt;here is jsFiddle&lt;/a&gt; with my solution for a problem.&lt;/p&gt;

&lt;p&gt;Enjoy&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qHGByDRU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/176/0%2AXZJQkYt4TbPLmWCO.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qHGByDRU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/176/0%2AXZJQkYt4TbPLmWCO.jpg" alt="javascript-book"&gt;&lt;/a&gt;&lt;/p&gt;




</description>
      <category>javascript</category>
      <category>web</category>
      <category>frontend</category>
    </item>
    <item>
      <title>KnockoutJS simple truncate text binding handler</title>
      <dc:creator>Dima Kuzmich</dc:creator>
      <pubDate>Mon, 24 Jun 2013 09:07:05 +0000</pubDate>
      <link>https://dev.to/forforeach/knockoutjs-simple-truncate-text-binding-handler-4jmg</link>
      <guid>https://dev.to/forforeach/knockoutjs-simple-truncate-text-binding-handler-4jmg</guid>
      <description>&lt;p&gt;Hello everyone,&lt;/p&gt;

&lt;p&gt;On my current project I needed to truncate text in some titles. Since we’re using KnockoutJS, I decided to write custom binding handler. And so I did.&lt;/p&gt;

&lt;p&gt;Custom binding handlers are great solution, that provides you with ability to control how your observables interact with DOM elements. In addition you encapsulate your custom behaviors, thus they became reusable. It’s very useful when you use third party widgets that need to be updated all the time when observable values are updated.&lt;/p&gt;

&lt;p&gt;So here is an example of my truncate binding handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ko.bindingHandlers.truncatedText = {
 update: function (element, valueAccessor, allBindingsAccessor) {
 var originalText = ko.utils.unwrapObservable(valueAccessor()),
 // 10 is a default maximum length
 length = ko.utils.unwrapObservable(allBindingsAccessor().maxTextLength) || 20,
 truncatedText = originalText.length &amp;amp;gt; length ? originalText.substring(0, length) + “…” : originalText;
 // updating text binding handler to show truncatedText
 ko.bindingHandlers.text.update(element, function () {
 return truncatedText; 
 });
 }
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can try it &lt;a href="http://jsfiddle.net/dima_k/bZEQM/1/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Use this binding handler and use binding handlers in general.&lt;/p&gt;

&lt;p&gt;Enjoy.&lt;/p&gt;




</description>
      <category>javascript</category>
      <category>web</category>
      <category>knockoutjs</category>
    </item>
  </channel>
</rss>
