JavaScript encapsulation & the module pattern

Encapsulation is one of the key features of object oriented programming languages. In languages like Java, it is very straight forward concept to implement. Since I know JavaScript is considered an OO language, I decided to try to understand once and for all how to implement encapsulation in JavaScript correctly. Let’s say we have a function with one private field and one public field: Try It The fields are of course undefined since we need to call f1() in order for them to have values. Now let’s create an object from that function: Try It After instancing, x and y both have values, but of course, only y is public. Now lets encapsulate field x. Try It This is the most basic encapsulation and it works. But this approach has a problem, f1() is in fact a kind of constructor. Whenever an instance will be created than the getter method will also be created. This approach is not the preferred way to create methods in JavaScript. The preferred approach in JavaScript is to create the methods attached to the prototype. In this way they will only be created once. But this approach also has a problem, we cannot use it Continue reading JavaScript encapsulation & the module pattern

Chrome extension: Right-To-Left display in Google Calendar

I love Google Calendar, but one thing bothered me about it; sometimes I add events with Hebrew and sometimes even a mixture of both English and Hebrew. Google Calendar doesn’t support it, when creating/editing events, it displays it in Left-To-Right order. That’s why I have created this Chrome Extension which works great for Hebrew and Arabic.   View on Github   Download Source code *Google Calendar is a trademark of Google Inc.

How to change pages in a web application in the age of HTML5

More and more web sites stop to use the old way of request-response page switches, where each page switch refreshes the entire page in the browser. Instead they refresh just the data and the page itself is not reloaded. This way has better for performance and it also makes a much smoother user experience. You can see a great example in github where you click on one of the files. In this post I am going to explain how this is achieved step by step. My Demo Requirements: For some reason, almost all of the examples I saw on the internet about this used jquery in one way or another. This post is NOT using any third-party libraries except for history.js script for browsers which do not support the history.pushState feature. I took this script from github. Step 1 – handle links We want that pressing a link will trigger a JavaScript function. You can do that in many ways. I have done it like this: It will go over all the links in the page and will attach a call to function goto(), but it will do it just for the links which points to inner pages. The goto() Continue reading How to change pages in a web application in the age of HTML5