Back to the Future !

(language: Odiya) Khanda pralaya ru khanda pralaya parjyanta : (language : Hindi)Quayamat se Quayamat tak....

Long long ago, there were 5 lions staying in a jungle with their families. A 5000 strong grazing animal population was part of the food chain and there was no problem for sustainability. Lions were on hunting spree when they were hungry; else they were uncles to everybody. Climate was good, no dearth of grass, enough water in the river, many fruits in the trees with lot of monkeys of course!! One day a little bird said to one uncle Lion that two strange animals are coming towards their jungle. They walk on their rear limbs, have no tails, eat anything eatable, stay inside a self-made nest, and use tools to do simple things to very complicated things. They have killed many animals on their way including an elephant by using their strange tools. They use some vocals to communicate. They preserve food for bad weather and use a rolling cart for transportations. I think they are dangerous. They are coming to stay here….for ever. The lion said, “I am the king of this jungle. Don’t you know which protein I eat? Elephant is a weak grass eating animal. Let them come I`ll pounce those walky talkies. I am very cruel when I am hungry. Everybody knows. They are not coming to my jungle but to my empty stomach”.

The lion waited and waited, they didn`t turn up. After few days he became very hungry. He thought those guys ran away to some other place. He saw a big fat immobilized spotted deer and thought for a great dinner instead. It was a trap. The two walky talkies killed him. Similarly they killed all the lions and started living there. With time their population grew. There were no lions, so they were on the top of the food chain. They developed medicines from herbs, used fire for cooking and many new things which surprized other animals. They overwhelmingly killed the grazing animals, even if they were not hungry! After few hundred years, grazing animals reduced to double digits. So they cleaned up jungle for agriculture, domesticated the leftover animals. After few hundred years they required more land for agriculture. A fraction of them migrated to the other side of the river and applied all the techniques they knew from this side of the river. After few hundred years, they started looking differently across the river. After few hundred years they had their own vocal communications, customs and Gods somewhat similar and somewhat different. Each side of the river started treating them as a better species. After few hundred years their number became huge. No more jungle no more grass land remained except the desert. At that point of time, they had two options either to reduce their consumption or to produce more. But they could do neither because they were habituated to live in an expensive way and their resources were limited. So they started begging, then stealing, then robbing each other. But none of them worked. So each side across the river secretly decided to attack the other side. They secretly build up unique war-fares and thought that they had an edge. One stormy night, the war started. All the weapons used. Rest is history.

Somebody said, “We all are born to consume resources”. Consume responsibly to live bit longer.

Fork me on GitHub

It was an exciting travelling time.

After a small travel around the country, meeting old friends, making new friends, visiting old places, going to new places and what not, finally I am back home. So the delay and posting here. Though travelling could not hold me back from posting, but then I took some time off to be back again and posting here.
By the way, thank you all who are visiting it, I must say the amount of crowd I am getting here is really motivating. Please do leave a comment, they are equally valuable to me too :)

My on the move partners, thanks.

Well, lately I am travelling a lot arround India. Most of the time I want to be online and do my job as well of continuous blogging and studying stuffs I like.

I must thank my HTC wildfire phone which runs Android OS, Airtel prepaid with 2g and rooming, my MacBook Pro, and my phone hotspot. These things made me a complete mobile office, productive and fun. Though slow connection but I did almost all of my travel tickets, voice chat over Skype, through these only things.

Love you guys and thank you for supporting me at my need.

Posted from WordPress for Android through my "HTC Wildfire".

A visit to the startup, geekmentors studios.

I am fortunate to get an eye inside the action packed startup geekmentors studios. Must say its amazing to see how determination can make a movement. The place is full of energy and fun.
at geekmentors
Seriously for 3 days we are on an average sleeping 4 hrs without loosing energy.
at geekmentors
Wishing all the best to the team and thanking a lot for the hospitality.

Javascript : some things to keep an eye on Canvas

This came to me as a surprise, when we were doing the last tutorial. If you see the code in detail, we have the code below.

 
<script type="text/javascript" src="js/general.js"></script>
 

The above code is not on the head section of the HTML document! Rather it came after everything in the HTML document. I hope its for the Canvas does not get initialised before the code is executed. But moving the script to the last section of HTML document, it worked just fine.
I hope someone have got better explanation about the effect, but thought its better to share as this very thing took me a while to get it working. After all I am also learning now and sharing my experience at the same time.
The next thing to keep an eye is the code below on javascript

var canvas_one=document.getElementById('board_one');

The above code is pure javascript without utilising jQuery library. But the same thing can be achieved by the code below

var canvas_one=$('#board_one')[0];

The thing which went wrong for me in the beginning is that I was doing

var canvas_one=$('#board_one');

First I was hoping this should work, but it did not unless I access the zeroth element. Hopefully this also can be explained by some experienced developer, but then as we are going ahead in our learning its better to know how to get the element using jQuery library and its by accessing the zeroth element as below.

var canvas_one=$('#board_one')[0];

Hope that helps someone.

javascript : Inheritence by prototype

Once we got the basic information and know how, of the "prototype" property of a constructor object, we need some code to test it. So here is another all in one page test for the "prototype" property.
We have 3 constructors and then the prototype property of the constructors are defined as another object. Basically, if 3 constructors namely A,B,C are present, prototype of A is defined as an object created by "new B()", prototype of B is defined as an object created by "new C()". There are 4 different properties in all the objects. But the relationship is now as below

 
A.prototype is new B(); 
B.prototype is new C()
//so the relation is as below
A -> B -> C
 

That means, "prototype" allows us to make a relation between the objects. We will see that in a moment. Now all the properties of all the objects can be accessed through the object created by "new A()".
If you have not yet opened the all in one page test for this example, open it now. And click the buttons to see the properties accessed from one Object.

 
var ClassA=(function(){
	//------define-------
	var a,b,c,d,getA,getB,getC,getD,numberToBeAdded;
	//------configure------
	a='Property a';
	b=500;
	c='Property c';
	d='Property d';
	//numberToBeAdded=100;//prototype object can not access this property, instead the local property will be used
	this.numberToBeAdded=100;//With the use of "this" now its exposed to be accesed from its prototype object
	//------expose-------
	this.getA=function(){
		return a;
	}
	this.getB=function(){
		return b;
	}
	this.getC=function(){
		return c;
	}
	this.getD=function(){
		return d;
	}
	//
	/*
	this.getSum=function(userNumber){
		return userNumber+numberToBeAdded;
	}
	*/
});
//
var ClassB=(function(){
	//------define-------
	var e,f,g,h,getE,getF,getG,getH;
	//------configure------
	e='Property e';
	f=9;
	g='Property g';
	h='Property h';
	//numberToBeAdded=200;
	//------expose-------
	this.getE=function(){
		return e;
	}
	this.getF=function(){
		return f;
	}
	this.getG=function(){
		return g;
	}
	this.getH=function(){
		return h;
	}
	//
	/*
	this.getSum=function(userNumber){
		return userNumber+numberToBeAdded;
	}
	*/
});
//
var ClassC=(function(){
	//------define-------
	var i,j,k,l,getI,getJ,getK,getL,getSum,numberToBeAdded;
	//------configure------
	i='Property i';
	j=300;
	k='Property k';
	l='Property l';
	numberToBeAdded=300;//"this" exposes this number to be overriden by the object which will prototype ClassC(or this class)
	//------expose-------
	this.getI=function(){
		return i;
	}
	this.getJ=function(){
		return j;
	}
	this.getK=function(){
		return k;
	}
	this.getL=function(){
		return l;
	}
	//
	this.getSum=function(userNumber){
		//return userNumber+numberToBeAdded;
		return userNumber+this.numberToBeAdded;//using "this" will override the local property and use the property of the object which would be using this object as a prototype
	}
});
 

The code to use the prototype is defined as

 
//makes the object
var objC=new ClassC();
//makes the prototype magic
ClassB.prototype=objC;
ClassB.constructor=ClassB;
//makes the object
var objB=new ClassB();
//objB.constructor.prototype=objC;
//makes the prototype magic
ClassA.prototype=objB;
ClassA.constructor=ClassA;
var objA=new ClassA();
 

So how it works?!! This magic is simply possible by "prototype". For example, if we are trying to access one property in an object created by "new A()" and the property is not present in the Object, then javascript engine will look for the property in the "prototype" object of "new A()" and that in our case is an object created by "new B()" and if the property is not found in it, it will search in the prototype property object defined for this object, that is in our case is an object created by "new C()".

The code below shows, how we are accessing properties from the prototype object through the single object.

 
//calling own method
objA.getA();
//calling B's method
objA.getE();
//calling C's method
objA.getJ();
 

If you go through the code here, you must see that there is the use of the key word "this". This is simply kind of saying that this property is accessible from outside. In Java or Actionscript kind of languages, its known as "public" variables. Remember that, whatever variables need to be exposed to outside, must be defined with the keyword "this". Going through the code again will show you that, some properties are just defined with "var", while some other are defined with "this". Remember, "this" always points to the current object.

Now with these knowledge, we can very well override or reassign values to our objects, which are defined in the prototype object. That means, if some properties are defined in our "prototype" object and they are exposed or public variables, then we can again define the same variables in our Object and our object variables will be used for calculations. This is very interesting and sometimes get some time to understand too. Again, coming back to code, we have a function in our "prototype" object which adds some numeric value to another given number. Both the actual object and "prototype" object have got the value, which needs to be added. But if the prototype value is exposed with "this" then only the value inside function, which calls this variable with "this" will take actual value from the object not the value from the prototype! Thats because, "this" always refers to the current object.

Here is the source code for this example.

Hope that helps someone. Would love to listen comments, suggestions and insights, as I am still learning it.

javascript : prototype

Moving on with my study of javascript, the next thing is "prototype". Before that, lets say it three times, "Everything in javascript is an Object".

There are only 3 primitive datatypes in javascript! Though some refer as, there are 5 types, but all in all those are the only ones. These primitive data types are

 
string
numeric
boolean
//The other two are
null
undefined
 

Apart from these basic datatypes, everything else is an Object. Though there are predefined Objects in javascript with these primitive datatype names such as String,Number,Boolean,Null,Undefined respectively. Please refer this for a detailed understanding.
Well, the point here is everything is an Object in Javascript. Unlike other programming languages where a Class is needed to create an Object, Javascript comes with Objects from the beginning. The dynamic nature of the language allows us to do so many things in so many ways that there really needs an eye who can see the details, of how things are done.
We know from past tutorial that "prototype" is a property of an Object created with "new" keyword. What does this do? Well, this allows us to link Objects internally!! Lets take a pause and understand another very important lesson of javascript and object oriented programming in general.
In a Class based language(Java or Actionscript) it is thought as, "how to relate the Classes, so that something is possible?", where in a dynamic language such as javascript, it is thought as "how to relate the Objects, so that something is possible?" Read it for a number of times so that you will get a hang of the metaphor and thought process difference, in mind.
So the point here is the same, we have to relate the Objects itself but not the Classes(Anyway, we do not have classes here in javascript but can fake them though).
Coming back to our "prototype", its the property of an Object created through the use of "new". This "prototype" property is also an Object in itself. To test this, let us refer our previous post, and the all in one page test. Go down to the "Understanding Javascript class." box and click on the "what is prototype" button. The pop-up will say, its an Object. That's good, lets move on and click on "prototype test" button, now it alerts "undefined" that means, "prototype" is an object but not yet defined! So lets define a prototype and call the same method again. First to assign an object to the "prototype" property click on "initiate with custom prototype" button. That will assign a custom object as the "prototype" property. Now click on the "prototype test" button again, and this time it will alert with some value, other than "undefined".
Whoa!! Its that simple. Yes it is that simple and for the same reason many confuse with the concept. After all the world has become complicated to understand simple things :) :)

The source code of the example is here to download.
Happy coding.

javascript : Making a Library

This is kind of looking at javascript from a fresh perspective. It does not assume you to know anything about it. Knowing another scripting language is always an advantage to know things better but sometimes it may come to a negative effect. If you know anything before, its better to assume you do not know that :) so that we can get up to speed in javascript as quickly as possible.
I hope you have a basic understanding of how to include external javascript and css files inside an HTML document. If not, then please go ahead and follow the posts mentioned below for a quick understanding.
Introduction to HTML
Getting used to the terms
Getting into web design action

Lets setup our experiment with the files listed below

 
index.html
general.css
saumyaJS.js
general.js
 

Now, the code inside index file is as below

 
<!DOCTYPE HTML>
<html>
	<head>
		<title>Study_01</title>
		<link type="text/css" rel="stylesheet" media="screen" href="general.css"/>
		<script type="text/javascript" src="js/jquery-1.6.2.js"></script>
		<script type="text/javascript" src="saumyaJS.js"></script>
		<script type="text/javascript" src="general.js"></script>
	</head>
	<body>
		<div id="greet">
			Hello World.
		</div>
		<div id="greetPerson">
			Hello you, this is JS.
			<button id="showBtn" type="button">show</button>
			<button id="resetBtn" type="button">reset</button>
		</div>
		<canvas id="graphics" width="600" height="400"></canvas>
 
	</body>
</html>
 

Here we are clearly adding all the CSS and JS files into it. The "general.js" file is where we are going to write the experiment codes. The "saumyaJS.js" is kind of a library we are going to write so as to know the javascript basics and help the experiment. The first line of javascript library includes the JQuery library, if you are not sure about it now, do not worry, just remember that I have used it to select HTML elements from my index.html file. And the JQuery library itself is a huge project,so we will get to that in a future post may be.

I am suggesting to go ahead and read Keith's blog for getting the hang of javascript environment from a learner's perspective. He has done a splendid job of putting his 31 days continuous blogpost about javascript.
31 days of Canvas tutorials
March is JavaScript Month. And… Introducing WireLibJS

First and the most important thing is variables in javascript. They are by default "Global", that means if we define a variable as

 
a="name";
b=2;
c="message";
 

Then all these variables are global variables by default. That will allow anyone to access these variables from anywhere. Well, to make local variables we have to define them with "var" keyword. So the definition will go as

 
var a="name";
var b=2;
var c="message";
 

Well as you can already guess that there is no restriction in datatype for a variable. And the scope of these local variables are inside a function. In short javascript variables have function level scope. Now lets define a function and add some local variables into it,

 
myFunction=function(){
	var d=0;
	var firstName='saumya';
	counter = 1;
 
	alert('reset');
};
 

That means anywhere we need to call this function, we have to call it as below

 
myFunction();
 

Thats simple as we define a function with name "myFunction" and invoked it with "myFunction()". Thats how a function is called.
Now one thing to understand and know is, its better to add as little as possible in global scope. Its a best practice and saves a ton of headache later in the development process. Now in our previous example, we are adding the variable "myFunction" in the global scope. But the variables "d" and "firstName" are in local scope. But then the variable "counter" is in global scope!! Thats the scary part of it. Yeah, anything without a "var" keyword is exposed to Global scope.
The previous example showed how to make a function with a name. It is possible to make a function without a name also, those are called anonymous functions. And since we know, to call a function we have to use "()" at the end of a function, the code below describes how to define an anonymous function and call it.

 
//makes anonymous function
function()
{
	//Code for function
};
//make the anonymous function and call it at the same time
function()
{
	//Code for function
}();//look at the "()" at the end of the function definition
 

The problem here is, once the function is called and executed, everything inside the function along with the function itself is not accessible any more. In order to get a reference to the function itself, we need to store it with some name. Thats where the named function comes into picture, that we have seen already as to how to define a named function.
Next we want to make an anonymous function, call it and then save the returned value of the function. A function can return value by using a "return" keyword. Example of a function which returns a value is as below

 
var myFunction=function(){
return 5;
}();
 

Now we have a variable named "myFunction" with a value of 5. This is simple but as you can imagine this return type can be anything. The code below shows how to return an "Object".

 
var myFunction=function(){
return {one:'fistName',two:'lastName'};
}();
 

Now "myFunction" simply contains an object with properties "one" and "two". If we want to access these properties, the call would be as below

 
myFunction.one;
myFunction.two;
 

With these knowledge we can write something like below,

 
var saumyaJS=(function(){
	// --------------- Define -----------------------
	var a,b,c,d,e;//define properties and methods
	// --------------- Configure --------------------
	a='saumya';
	b='ray';
	c=function(msg){
		d++;
		alert(d+':'+msg);
	};
	d=0;
	e=function(){
		d=0;
		alert('reset');
	};
	// --------------- Expose -------------------------
	return {
		firstName:a,
		lastName:b,
		show:c,
		reset:e
		};//expose the properties and methods to outer world
}());
 

Here simply some local variables are defined, then configured them with values and finally whatever needs to be exposed to outside world, is retuned.
Whoaa!!! We have simply written a simple Javascript library.
If you look into the code a little bit, then its visible that the variable and functions inside the main function is accessible only by the names they are given at the time of returning. If one needs to access the value of "a" from outside then the call would be

 
saumyaJS.firstName;
 

But if one tries to access the value as

 
saumyaJS.a;
 

then one will get "undefined"!! This is how encapsulation works or simply, how one can hide values of an object in javascript.

This is a very basic concept of javascript, but I think this is the most important concept of javascript too.

Now we can see that whatever we want to access, we have to access through the "saumyaJS", everything inside that function is not exposed outside or there is no more than one variable exposed to the outside world.

The source files are here to download and see for yourself. Hope that helps someone.