Skip to content

javascript classes, prototype

April 3, 2008

onBlur to know the id of the next focus element – we can do that using….

You understand JavaScript closures and prototype based inheritance.

You understandXML/XmlHttpRequest in depth.

Ajax calls – ensure to expire the cache in page_load method.

private void ExpirePageCache()
Response.Cache.SetCacheability(HttpCacheability.No Cache);
Response.Cache.SetExpires(DateTime.Now-new TimeSpan(1,0,0));
Response.Cache.SetAllowResponseInBrowserHistory(fa lse);

First things javascript, a function is also an object.
You can declare a function like so:

var f1 = function(parm1) { }
function f1(parm1) { }
var f1 = new Function(body, args)

There is the concept of scope and prototype chains in javascript. If you are accessing a member of an object,
it first looks to see if that object itself contains the member, and if not, it goes up to that objects .prototype member from which it was created. So, first try obj.member and if that fails, try creator.prototype.member. Also, prototype type objects can be “chained” for more levels, but you don’t have to worry about that now.


var o1 = function() { this.y = 8}
o1.prototype.x = 5
o1.prototype.y = 7

var instance = new o1();

Now, instance.x = 5 and instance.y = 8 (not 7)
Since o1 is an object (not just a function), you also can attach stuff to it:

o1.z = 8
Now, instance.z is undefined. Since .z is not in o1’s prototype object from which o1 was created, it is not visible to instance.

Back to your code snippet – it is just delegating the “constructor” of the jquery object to a another function in it’s prototype object. Usually, the body of a function IS the constructor if you wish to make an object out of that function with the new keyword.

If you say jquery.fn = jquery.prototype.fn, then you can call function fn on the jquery object itself AND on instances spawned from jquery’s prototype (new jquery()).

It might differ among browsers, but the default object for everything in the global scope should be window. Also, the “this” keyword usually is a reference to the default window object as well (unless of course you are inside an instantiated object from new()).

Back to the first example, you can straight out call o1() without new

In that case, “this” should refer to window, so you would be setting window.y = 8.
var instance would then be null since it doesn’t return anything.

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: