Skip to main content

Posts

Showing posts from July, 2014

Prototype in Javascript

Prototype are link or pointer to other objects. All objects have prototype. Objects are chained together by the prototype chain.

//Constructor
function Office(name, location, domain)
{
this.name=name;
this.location=location;
this.domain=domain;
}

//Prototype


Office.Prototype={
    interview: function(){
alert("Hi office location is"+ this.location);
}
};

var myoffice= new Office("expo-e", "london", "telecommunication");

myoffice.interview; // hi office location is london

In the above example

myoffice prototype is shared with Office
Office prototype is shared with object
Object prototype is shared with null

myoffice->Office->Object->null, makes the complete chain of prototype.

when you lookup a property on an object, it will walk up the prototype chain until its found. First my office  is checked then the Office is checked, then object and finally it reaches null.

Javascript Closures

A Javascript closure is when an inner function is inside of an outer function (a function inside a function). The inside function has access to

1. Variables defined in the outer function
2. Argument passed into the outer function

var CreateExample= function(example){
                                    return function(text){
                                              console.log(example +" "+ text);
                                       }
};


var example1= CreateExample("Hi");
var example2= CreateExample("Hello");

example1("test one");   //Hi test one
example2("test two");  //Hello test two.

In most object oriented languages like java/c#, when you reach return all variables declared inside that function get destroyed and memory is freed up.

IN javascript, when creating a closure and return as function, all local variable will remain in memory and accessible.

Can we store wifi and use it later?

I started using WIFI almost 8 years back (in 2006) when I moved to the UK. Before that, during the engineering degree I studied about WIFI energetically for achieving grades. Nowadays it’s a common friend and every day without her presence, it is awful to envisage. One day my data allowance on the mobile run out and I want to go to an event in central London and want to use the GPS. I had to take map printout and follow the route. I depend a lot on GPS navigation rather than a paper map. So I reached the location half an hour later in the event. Eventually I missed the event and while walking back to the tube station I was thinking about storing the WIFI somehow and using it later, like a battery. People might think why can’t I carry a portable modem and a laptop, but I am thinking about much sophisticated device than that like a battery for the phone. Did anyone think about a life without batteries now? It is there since most of us on this planet born. In 1749 Benjamin Franklin firs…