Javascript – Is it Object Oriented or Prototypical ????

Is javascript Object Oriented or Prototypical language?

This was one of the confusing questions on which I spent a lot of time googling and discussing or rather arguing with my friends. And finally the I could reach a convincing solution which is as follows:

Javascript is an Object Oriented languages. At the same time it employs prototype based programming(which is special style of Object oriented programming) for accomplishing the concept “inhertance”. According to Douglas crockford, the renowned javascript Guru, JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance”

Prototype-based programming is a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is accomplished through a process of decorating existing objects which serve as prototypes. This model is also known as class-less, prototype-oriented, or instance-based programming.

Which means unlike other languages (eg java ,c++) it does not have “Classes”. But it uses functions as classes. In other words defining a class is as simple as writing a function. For instance,

function Animal(){

this.name = “lion”;

}

Whenever we create a new object using “new”, this method is invoked. So this function can be treated a constructor. For those who insists on classes, this function can be considered as class as well. As convention, Constructor name always start with capital letter.

Eg: var lion = new Animal();

Customizing Eclipse for Web Development

With the introduction of Android ADT bundle ,android installation has become an easy task as we can get both android and eclipse installed in a single step. But  now as we all now hybrid applications have started to rule the mobile application world, and hence handling javascript and HTML files using the same IDE what we are using for android development makes sense. Yes, eclipse can be customized for web development  by the following steps.

Step 1

Open eclipse. Click on Help menu. Select Install New Software. This would take us to the following screen.

Screenshot from 2014-03-19 23:09:18

Step 2

Click on add button. It would lead us to the screen placed below.

Screenshot from 2014-03-19 23:08:58

In the name field just give any repository name, for example –  web development.

for  location type the url to eclipse plugin site of appropriate version. Since I am working on eclipse juno,  the location field contains the following url:

http://download.eclipse.org/releases/juno/

Then press OK button. This will take us to the previous screen.

Step 3

The above step the make the plugins for juno version available for us. Now we need to choose the appropriate ones that meet our requirement. ie plugins for html javascript development. For this do the following steps.

Help ———> Install New Software

In the resulting window you can see Work With field. From its option list select the one we have just added, in the previous step. ( here web development –  http://download.eclipse.org/releases/juno/ ). This will lead us to the following screen. Select the plugin for web development only.

Screenshot from 2014-03-22 13:48:42

Step 4

Now this selected option gives rise to a number of sub options. Choosing all sub options won’t be a good idea. Just choose only the required ( for html and javascript development)  plugins for the installation.

Select Eclipse Web Developer Tools and Javascript Development Tools.

Click Next button.

Screenshot from 2014-03-18 22:41:59

Step 5

Now we will get following screen.

Screenshot from 2014-03-18 22:42:17

Now click the Next Button and you can see  a screen like below.

Now accept the conditions.

Screenshot from 2014-03-18 22:43:12

Click on Finish button. and now you can see that the softwares are being installed.

Screenshot from 2014-03-18 22:43:28

Step 6

Now everything need for HTML and javascript development are installed in our eclipse.

just Restart the Eclipse

Creating first web project in Eclipse   

1. just go to File menu. Click new.

2. Click on Static Web project. If you don’t have such options just  click on Project and select Web and then click Static Web project.

3.  By default the Eclipse IDE would be in java perspective. We need to switch it to Web perspective. On the top right corner of the IDE you will be able to see Java and Web perspective options. Just click on the Web perspective, in order to make it active.

1

Now proceed with project creation and development just like what you have been doing in Eclipse for android or java.

*******************

Undefined vs Null vs NaN – Javascript

If you have tried to develop atleast one application in javascript,  you might have come across these kind of things.  (probability is 1..though my knowledge in probability is really pathetic). I still remember the confusion I had regarding these three terms, when I stepped in to the world of javascript.  The following are my assumptions about  the perplexing trios in javascipt — undefined, null, and NaN.

undefined  :  Th-is can act as both a type and a value. A variable that has been declared and has not been assigned  with a value is undefined always.

For example –

var tmp;    //  just declared, not assigned with a value

console.log(tmp) ;    ———————-> undefined

console.log( typeof tmp );———————-> “undefined”

From this it is clear that value of tmp is undefined. and its type also is undefined. but this is an undefined enclosed by quotes.

Null    : is a  primitive value in javascript  whose type is Object.

var a=null;

console.log(a);  —————————-> null

console.log( typeof  a );  ———————-> Object

Still there will be some confusion for some of you. I had undergone this phase. So I would like to summarize it as follows.

Undefined  comes into play  when javascript does not know much about that variable. ie Its type is not known. It has never been referenced before.  javascript is not  even sure about its existance. On the other hand null comes into the scenario when a variable is known to exist(type is known). But its value is not known.

NaN (Not a Number)

It is the value returned as a result when an invalid arithmetic operation is performed. Its type is Number. It is value.

For example :

var  tmp = 10 / ten                    // invalid arithmetic operation

console.log(tmp)   —————————>  NaN

Now we need to understand another important concept about NaN ………………….

                                  Two NaNs are not at all equal in normal comparisons!!!!!!!

This would be clear from the following example.

var  tmp2 = 10 / ten                    // invalid arithmetic operation

console.log(tmp2)   —————————>  NaN

if (tmp == tmp2)   ——————————–> false

Normally what we are expecting for above operation , is a true value. But Its result is false. But What if we need to compare two NaN values? For that purpose we need to make of the built in method isNaN() . It returns a true value when a NaN value is fed as an input.

For example, in the previous cases we have seen

isNaN(tmp)  ——————————> true

isNaN(tmp2) ——————————-> true

For comparing these two NaNs

if ( isNaN(tmp) && isNaN(tmp2) )   ——————————> true

Now we can compare two NaN values successfully.