Tuesday, March 24, 2009

Download JavaScript Gridview

I have developed a JavaScript Gridview. You can download and use it in your website. It is very customizable for which you just have to change css styles. And it can sort the data rows columnwise(eg. sort by name, DOB) by just clicking on the header row. I will release the next version with more additional functions like paging. Please leave the comments how you like it?

download

Friday, March 13, 2009

Fundamentals of Prototype Object in JavaScript

[click on the picture to enlarge it]

I got a chance to read the specification of the JavaScript where I learned about the fundamentals of object and its prototype object. We create an object in JavaScript by
function obj(){
this.prop1=sth;
}
When we create an object, JavaScript also create a “prototype object” for that particular object.
By default, JavaScript will also create properties like .constructor (To learn about it, I would like to recommendhttp://www.w3schools.com.) and .prototype. This .prototype property of the object points to its prototype object.
Now, when we create an instance of the object by using new keyword, it makes a replica of the object. The instance has a copy of properties as well as methods in itself.(unlike other object oriented programming language like c++, Java where the instance keeps a copy of properties only but methods remain with the object and all instances share the same methods).

instance1= new obj();
But all the instances of the object share the same prototype object which means all the properties and methods in the prototype object are shared by all the instances.

For example:
obj.prototype.sharedprop1=”sharedvalue”

document.write(instance1.prototype.sharedprop1)
Output: ”sharedvalue”

document.write(instance1.prototype.sharedprop1)
Output: ”sharedvalue”

So, if the methods and shared properties/variable are put in prototype object, the instance always consumes less memory. If you are creating large number of instances of the object, you really have to work on with prototype object.

If you write document.write(instance1.sharedprop1) it will still work and gives output “sharedvalue” because what JavaScript does is, when it doesn’t find out the sharedprop1 in instance1, it lookups for that property in prototype object and get it. In the same way it works for methods also.
Links:
http://www.ecma-international.org/publications/standards/Ecma-262.htm
http://www.slideshare.net/s.barysiuk/javascript-basics-and-dom-manipulation-presentation