Saturday, July 19, 2014

Difference between absolute and relative position

One of my student was asking  differences about absolute and relative positions, So here some points I have described.                                                                                                                                      

Figure
Source Code

Absolute Position-

When you apply the absolute position to particular element, it would be detached from current flow.
Absolute position is the position which is related to nearest ancestor relative element, If there is no relative ancestor of absolute-positioned element, then it's related to document element.

For example at above figure, we applied absolute to green div,  and green's nearest ancestor relative element is orange div, so left and top of absolute position of green div would be apply from orange div.

If there is not applied relative position to orange div then the ancestor element would be document tag and the left and top value would be apply from document element rather than from orange div.

Relative Position:-

It respects the flow, when you apply the relative position to particular element then the left and top value would be apply from where this element originally is existing.

Like the blue div is existing at below the yellow div, After applied the relative position on blue div it would be moved from where(From below the yellow div) it is existing.

Saturday, July 12, 2014

About prototype

What prototype is?

Prototype is an object, in JavaScript, a constructor(function) has a prototype which has it's properties, and methods.

These properties and methods would be inherited to instance which is created from particular constructor. Here we go with the example,

var Transport = function (){
this.name = "car";
this.wheel = 4;
}

Transport.prototype.run = function (){
console.log("The " + this.name + " " + " run fast");
}

tp = new Transport();
tp.run(); //this car run fast

Prototype-Figure:-1 

Here  the method run()  is defined through the prototype to Transport constructor.

Why we need the prototype in this way as we can do simple like this.

var Transport = function (){
this.name = "car";
this.wheel = 4;
this.run = function (){
console.log("The " + this.name + " " + " run fast");
}
}

tp = new Transport();
tp.run(); //this car run fast

Every time the Transport is invoked this.run has an instance of Function constructor, For each instance of Transport, there would be  each different instance of Function for run method.

In simple words, If we have instances of Tranport() like

tp1 = new Transport();
tp2 = new Transport();

tp1 and tp2 have different instance of Function for run method which means
tp1.run == tup2.run is false.

Where the run method is performing same behavior for both instances tp1 and tp2.

So  at Prototype-Figure:-1, we defined the run method through the prototype object, in this case the run method would be shared over the instances tp1 and tp2.
Which means tp1.run and tp2.run have same instance of Function for run method in this yo case you could say.

tp1.run ==  tp2.run //true

Think about thousands instances are made from Transpor(). If we don't use prototype concept then thousand different function instances would be created for run method to thousand instances.

If we use prototype then there would be only one function instance for run method when the instances of Transport are created.


We have illustrated this concept with below figure,


Under the part of "before use prototype", you can see three object instances.

Each new instance of function is assigned to run method of particular object, so there is three instances of new functions are assigned to run when three objects are created. If there is thousand objects then there will be thousand new instances of function  which is not god.

If we assigned the run method with prototype object of constructor then various instances  point to the same function run from prototype object, as you can see  at part under "After user prototype", three instances(Mercidies, Ferari, hondaCity) are sharing the same method run.

So performance wise it does matter when the objects in thousands or more.

Prototype for inheritance.

Till EcmaScript 5, JavaScript does not have class concept like PHP, Java. To achieve this concept in JavaScript, Prototype is very useful.

Inheritance is important concept for Object oriented Concept. Below example show the inheritance concept in JavaScript.


var Transport = function(){
this.motion = "movable";
this.move = function(){
console.log(this.name + " does move according to speed and its " + this.motion);
}
}

var Car = function (name, color, price, sound){
this.name = name;
this.color = color;
this.price = price
this.sound = sound;
this.sounding  = function (){
console.log(this.name + "is souding " + this.sound);
}
}

Car.prototype = new Transport();

var hondaCity =  new Car("honda city", "black", "$10000", "smooth");
hondaCity.move();
hondaCity.sounding();


We have a super class Transport and sub class Car, As you can see by this line

Car.prototype = new Transport();

We have inherited the property 'motion' and method 'move' from Transport into Car. So Car has the property motion and method move that's why hondaCity.move() display the "honda city does move according to speed and its movable".

So prototype can be use to achieve inheritance concept in javaScript.

Still there is problem in above example, as above discussed there would be created the multiple function instances when we create the various instances from Car constructor. Here is the improved version.

We split-ed the move function from constructor defined by located outside from Transport constructor like

var Transport = function(){
this.motion = "movable";
}

Transport.prototype.move = function (){
console.log(this.name + " does move according to speed and its " + this.motion);
}

var Car = function (name, color, price, sound){
this.name = name;
this.color = color;
this.price = price
this.sound = sound;
this.sounding  = function (){
console.log(this.name + " is sounding " + this.sound);
}
}

Car.prototype = new Transport();

var hondaCity =  new Car("honda city", "black", "$10000", "smooth");
hondaCity.move();
hondaCity.sounding();

How prototype chain works

When we  use particular property/method on particular instance, then first of all it would be search on that instance, if found then okay otherwise search operation would be perform in prototype of constructor from which that instance is made, The search would  be continue to upper level until the search is found or top level constructor is found.

Here we go with example,

As we know every instance has internal pointer to prototype object and  prototype object points to the constructor,  so what if prototype object has instance of another constructor. In that case prototype object points to the prototype of another constructor.

Here we go with above example.



Car.prototype has an instance of Transport constructor so it does point to prototype of Transport.

So when we execute the hondaCity.move() first it would be search on hondaCity, not found then it would be search on Car prototype still not found now it would be search on prototype of Transport now it is found and executed.

Saturday, April 19, 2014

Unit testing with sinon.js

During the unit testing of javaScript project, I came to know about sinon.js, I have not fully explored about this, however want to share some basics on this. So

What is Sinon.js?

Sinon.js is moking framework, which makes your test automate. The framework is made by javascript and it  has lots of feature like:- spy, stub, fakeTimer, fakeXhr and fakeServer. Later in this article, I'll describe about some of them.

Why we need Sinon.js?

You definetly want to make your unit test automate as much as you can. Making test automate means it does not depend over other test. The test will be performed alone for result pass or fail. During the unit test at some point you need to external resource for your testing, like you need to validate to response of ajax request. Or you want to validate the reponse which is came from server and you may need to test callback function. For that you need mock external resoruce, Eg:- fake server which does response your data , fake xhr which does response your request.

So basically sinon.js makes test is easy, maintainable and automate. Lest have some explore about it's feature.

Spy
Spy is a function that stores the arugments, it's calls, returned value, Through the spy you may know, how your function is working in your system, like how many times it's invoked, with which arguments its invoked. Lets take a look example how its worked.

For testig I am using qUnit framework

function add(a, b){
    alert(a + b);
}

test("Fake server is working", function () {
    var callback = this.spy(add);
    callback(10, 20);
    ok(callback.calledOnce);
    ok(callback.calledWith(10, 20), 'called with arugments');

});

First test is passed because callback() is invoked only once
Second test is passed because callback is invoked with 10, 20 arugments.

Stub
Stubs are like spies with predefined behaviours, which means you can use stub when you want to force the function/method to think the way you want. It can use all the api of spies. When you don't want to invoke direct function/method for testing, at that time you can use stubs,

Here is the simple example.
test("Fake server is working", function () {
    var callback = sinon.stub();
    callback.withArgs(42).returns(52);
    alert(callback(42));
    equal(callback(42), 52);
});

Fake server
You can create the fake server to response the data of request to server from your browser, and you can validate that resopnsed data.

Here how you can create fake server.
test("data from server", function () {
   var server = this.sandbox.useFakeServer();
    server.respondWith("GET", "/mytest",
  [200, { "Content-Type": "application/json" },
      '[{ "id": "52", "name": "Rock" }]']);
var callback = this.spy();
$.ajax({
url: "/mytest",
success: callback
        });

server.respond();
equal(server.requests.length, 1, "server request length");
ok(callback.called, "spy called once"); //failed
ok(callback.calledWith([{ id: "52", name: "Rock" }]));       //failed
});

Here we are calling the ajax function, and fake server is responding expected data.
By calledWith() method, we are cheking the relative paramters are passing on succesfull callback function. Here is  LIVE DEMO

Ofcourse I will update this article when will grab more about this topic.

Unit testing with javascript

UNIT TESTING

Well, I wrote some unit test for my project before six months, After that, I had left it and I had been working on the code without unit testing, I realized it's really hard to maintain the code without unit testing, Lots of time I am consuming for manual testing and maintain the code as it's complexity is increasing , So I want to make my code very maintainable, modularize with automate testing by using unit test.

So in this context I want to write some basic points about unit testing,

Why we used unit testing?
First of all unit testing means you have to test each unit of your code, which means your code would be very modularize, for write unit testing you need to put your code into particular function, and the function/method would be tested, we expect to do particular action by that function.

Whatever problem will be occured in your application, you would know on which function/method it's affecting.
You can make changes code without worry about it's bad effect into code because after changes the code you can run all the test at once and can see it's effect on partcular part of the code. You can fix these issue by working on particular code if there is any.

I am using the qUnit testing framework for unit testing of my application, which is very simple to use, It has various assertion.

Main asserstion

Check for for boolean(true, false) value
ok(true);
This assertion is passed when the condition is true.

Check for value equalness
equal(fval, sval) 
This assertion is passed when fval and sval are true, fails if these are not equal

Check equalness of value and data type.
deepEqual(1, "1");
This assertion does check it's data type also, above test would be failed.

And also it is used to compare the object like,
deepEqual({fname:'suman'}, {fname:'suman'}); 
The above assertion would be passed.

When you need to do test asynchronous function then you can use asyncTest(), For example you have a replay() function which is executed for 2 seconds. You can write the code for testing like this.

asynchTest("testing for replay", function (){
    replay();
    setTimeout(
    function (){
//this lastId is last executed id from replay();
equal(lastid, 20, the last id should be 20);
}, 
2100
    );
});

Here we invoked the replay() function, after execute this function with 2 seconds, the value of lastId should be 20, so we would check this value by running test after 2.1(2100) seconds.

As I will grip more about this will update the article.

Saturday, March 29, 2014

About Branch in Git

What is branch in git.

Earlier  I did not know about branch, probably still need to more explore about this, however I am going to write some words for this.
Branch is a separator, created for  particular task while we are working on git. Every project has master branch, various task, improvements, bugs in git.

We created the sub branch for each new task, after completed this task we will do merge that branch along with the master branch.

Why we need to do branch?

Suppose we have a project, "File Tracking System", Now there is a bug  raised #213 and one of the developer is fixing this bug with created sub branch named 'bug-213', We supposed there is up-do-date code existing into git, and at the same time, there is critical new bug is came and need to fix that first, We'll have to work on this issue first and should work on other bug at later.

Now we do switch to main branch from working branch('bug-213') and fix that new bug and update it into git. Now we would like to work on earlier bug #213 so we do switch back to branch 'bug-213' from master branch. After fixed this problem, we will do merge this working branch to master branch.

One point need to be careful here that if  same file is modified in branch 'bug-213' and in a file(during the fixed of new bug) then there would be the conflict during the merge, we have to resolve that conflict first and do merge this branch 'bug-213' to master branch.

Here how do we create, switch and merge the branch.

For create a branch named bug-213
git checkout -b bug-213

For switch to branch bug-213
Switched to a new branch bug-213

For switch to master branch
git checkout master
Switched to branch 'master'

When you are in master branch run this command for merge bug-213 branch to master branch.
git merge  bug-213

After merged the branch successfully, you can remove this branch by

For remove the branch locally
git branch -d bug-213

For remove the branch remotely
git push origin : bug-213

I will do update the article when will do more explore more about topics.

Saturday, January 18, 2014

HTTP definition

HTTP is stands for Hyper Text Transfer protocol, It is a application layer protocol under TCP/IP. Basically it used for send the request from client to web server and it displays the response in relative format which is arrived from server into client.

For example if user type some URL into location bar of browser then through the HTTP client sends the request to Web server, and display the particular content in browser which is responsed by web server to client.

HTTP ensure that how the request send to the server and how the responded data would be display into client.

For example in which format the request from browser should be send to server, Is it GET, POST, or HEAD, which is ensured by HTTP.