Sunday, March 17, 2013

Difference Between Protoype and __proto__ in Javascript


So what’s the difference between __proto__ and prototype property in Javascript?


The prototype is used to create __proto__  Simple!

Yes, I know. That statement does not help much in terms of explaining what is going on :) But stick with me, the plan is to make it clear by the end of the post.

So let's dig into explaining this stuff!

The subject of prototype is related to Javascript objects and object creation and relation; so before going further, I recommend first reading Understanding Constructor Function and this Keyword in Javascript as this would provide some basic information that would help in understanding some of the basic concepts needed.


Saturday, March 16, 2013

Understanding Constructor Function and this Keyword in Javascript


Back in 2006 I wrote a post on using constructor functions in Javascript to create objects. This post builds on that post, expounding it a little bit further.

By definition, a constructor function is a normal Javascript function. Nothing more, nothing less. What makes a function a constructor is how it is used. When a function is used in such a way that it can create an object, by calling it with Javascript's new operator, then it becomes a constructor function.


This means all and any function you have in Javascript can be used as a constructor function.

var Person = function () {
  console.log("I am a function");
}
We can call this function normally:
Person(); //prints to the console "I am a function"
console.log(typeof Person) //prints to the console ‘function’
So let’s now use this function as a constructor and create an object:

var bond = new Person();
Now let us confirm that bond is indeed an object:

console.log(typeof bond) //this prints "Object"
Now that is all about Constructor functions.  It is as simple as that: A function that is the same as your normal Javascript function but can be used to create objects.

But you would notice that the object created above is empty (without properties) and maybe of little or no use. Compared to the example given in my previous post, which had properties. 

So how do you actually make an object with some already set properties at creation? Before we go ahead and look at this, its worth mentioning a characteristics of functions in Javascript (apart from this fact that they can be used to create objects).
Functions are first class citizens in Javascript and also Functions can have properties just as objects! Yes just like you add properties to an object, you can do same to a Javascript function.


Friday, January 04, 2013

Now You Know What I Did Last Weekend: @redditJava




I am a regular reader of Hacker news: It’s a great place to discover interesting stuffs. But I don’t go checking http://news.ycombinator.com every now and then, neither do I subscribe to their RSS feed. I get most of the stuffs I read on Hacker news via twitter. There are Hacker news’ bots that I follow on twitter which provides the utility of having *hot* articles tweeted once they have attained a certain number of points or comments. Namely: @newsyc150, @newsyc100, @newsyc50 and @newsyc20.

So recently, when I decided to pay a little more attention to Reddit, the first thing I looked for were twitter bots I can follow that would feed me with interesting contents from Reddit. One nice thing about Reddit, unlike Hacker news, is the fact that Reddit have sub categories or channels which are called subreddits which cater to specific topics. You got subreddits for Politics, Gaming, Movies, Music…the list is almost endless. So I basically was on the lookout for twitter bots for subreddits I may be interested in.

But my searched returned null.

Since I could not find one, I decided to make one for myself. And I decided to make one for the Java subreddits since I recently started playing with the language again. (By the way having the bot track other subreddits is simply a matter of configuration).

The idea is simple enough: get stuffs from Reddit, have a way of getting only the interesting ones based on the number of points and comments, and then tweet only those. Also, make sure you also don’t have the same thing tweeted twice. The content is there, the APIs are there, so I set about getting this done within a weekend.

For this, I used Node.js. Reason? Well, I spend a lot of time with Javascript now, notable because I work with the language at work, building the world smarter customer service app, which enables business to keep more happy customer: Casengo; and since Javascript is very much capable, why not?

It was fun hacking this bot together the last weekend as I also took time to play around with stuffs: For storing information to prevent double tweets, mongoDB was used; this allowed me to get pretty conversant with the idea of document based DB and how it works in mongoDB. I also stumbled on node-supervisor. A handy Node Module which prevented the need to manually stop and start the server on every change in the code. Use node-supervisor to run a node script, it watches for any changes in your file; on change, it automatically reloads the script. This definitely made the development flow smoother.

I also checked up on writing Node Modules while following generally accepted guidelines especially if you want your module to be compatible npm. Also learnt one or two things about setting up mongoDB on AWS’s EC2.

Apart from the obvious Twitter and Reddit API’s I equally got to play around with bit.ly and goo.gl API’s.

And finally once deployed, I used forever to keep the script running. Forever ensures that a node process continuously runs and automatically restarts itself when it exits unexpectedly.

Javascript is definitely a pleasant language to work with. But you could trip if you fail to wrap your head around some of its unique and basic characteristics. First all, it is a Prototyped based OOP language (not class based) it is also event driven and easily supports asynchronous programming. This alone could result in some unexpected behaviour if you do not put these unique characteristics in mind. An example, where one can easily be tripped would be handling asynchronous API calls inside a for loop. (More on this in coming post) You also have things like variable hoisting, scoping, closures, the abstruse this etc., all which may make the language appear weird at first approach.

It was a weekend well spent and would spend some more free time modifying things and perhaps adding more subreddits that catches my fancy. The bot is already up and running by the way and you can follow on twitter here www.twitter.com/redditjava and the code is hosted on github here https://github.com/dadepo/redditTwitterBot.git

Tuesday, August 28, 2012

Understanding Zend Framework’s Plugin: Resource Plugin...(Part Three)

This post on Resource Plugin would be the last in the series of posts on Plugins as we have it in Zend Framework that I started a couple of weeks back. The previous two posts were on Simple Class/Front Controller Plugin and Controller Action Helper and View helpers.

Resource Plugin are tied to bootstrapping process in Zend Framework. In order to get a good understanding of Resource Plugins, it would be good to first of all take a step back and have an overview of what Bootstrapping means in Zend Framework’s context. This would help in getting a better understanding of what actually goes on when we say we want to make use of or write our own Resource plugins.

So what exactly is Bootstrapping?

Whenever a Zend Framework application is accessed, the following process must occur in order for the application to run and respond to a request. These processes occur in the order that they are listed below:

1.Application Bootstrapping: This is the process of preparing the application and making sure things needed to run it are available: setting up application environment etc.

2.URL Routing: Process of getting the URL and determining the respective Modules, Controller, Action that is being referred to in the application.

3.Dispatch: This is actually the process of initiating and calling the necessary controller and action from the specified module (if module is specified)


Sunday, August 05, 2012

Understanding Zend Framework’s Plugin: Controller Action Helper Plugin, and View Helper...(Part Two)

Last week I started off with the first post in a series of post on plugins as you have in Zend Framework. This is the second part of the series, where I would look at Zend framework’s Controller Action Helper and View Helper, since the previous post covered Simple Class plugin and Front Controller plugin - you can read it here.
As I said in my previous post, the idea behind these series of posts is to shed some light on plugins and the different incarnation that exists within Zend framework;


Controller Action Helper

First let me start by explicitly saying that Controller Action Helper and Front Controller Plugin are different, although they may sound alike. Read about Front Controller Plugin here.

Controller Action Helpers are the kind of Plugins that are tied to your Controller Actions, and just as Front Controller plugin, they can run specific tasks at some predefine stages of the MVC cycle; though this isn't mandatory. The key difference between Front controller plugins and Controller Action Helper is that your controllers can interact with action helpers to change their behavior, or use some on-demand functionality while this is not the case with Front Controller plugins.



Sunday, July 22, 2012

Understanding Zend Framework’s Plugin: Front Controller Plugin, Controller Action Helper, View Helper, Resource Plugin...(Part One)


This post is the first of three posts on Zend Framework's plugin. It would give a quick overview of the concepts of Plugins in Zend Framework. The aim is to provide an easy and short explanation of various plugin related concepts in zend framework; making it easy for a new comer to get up and running in no tim.

Usually, after getting the basics of Zend framework; that is: setting up your projects, using the Zend_Tool,  using controllers and views etc, more often than not, the next points of confusion for the new user usually stems from having to grapple with the various minor concepts within Zend framework.

An example of these concepts is the idea of Plugins. Plugins by itself is a simple concept right? But within Zend Framework, there are so many variant of plugins that it could quickly get perplexing to a new user.

What is a Front Controller Plugin? What is a Resource Plugin? What about our View Helpers? How are these different from Controller Action Helper? Wait. There is a Controller Action Helper? How is it different from our Front Controller Plugin? As you see...it could get confusing pretty fast.

Like I said in my previous post on how to set up a modular application structure, zend framework itself is not inherently difficult, just that it could take a while to wrap a head around how everything fits together, plus the paucity of useful and up to date resources out there does nothing to help matters.



Monday, July 02, 2012

Introduction to Git and GitHub

A presentation I made for an even earlier this year on Git. The presentation covers the basic work flow of init, clone, add, commit and push. Other commands like git remote, git pull etc are briefly touched.