Saturday, January 5, 2008

Next! - Prototyping

Hi again, here is my second post about my library!

Well this post is not talking about Next! directly but talks about my way to look at JavaScript.

Theres allot of buzz about un-obstructive JS, they mean that we should not extend core objects and we should put everything in packages, and don't pollute main scope ( window ) with variables etc...

I agree in some aspects, but i don't like to loose the main advantage of JavaScript, extending Core objects, ok that makes my library breaks compatibility with other libraries. Thats bad, i know.

Lets look on this, for start my library i do:
var next={};
For now on i put all my vars and functions on that context, if i do everything in un-obstructive way i would have todo something like this:
//un-obstructive
next.String={};
next.String.startsWith=function(string,what){
return string.indexOf(what)===0;
};
//for use it we do:
next.String.startsWith("test","t");//true
//obstructive
String.prototype.startsWith=function(what){
return this.indexOf(what)===0;
};
//for use it we do:
"test".startsWith("t");//true
Now... doesnt the second aproach look better and eseyer to create and use?
Not only to talk about where we can resolve missing stuff in diferent browsers!
For example JavaScript 1.6 suports some new array extensions:
http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6#Array_extras
They are great! But not all browsers suport them yet, so we can create them for browsers thatdot suport!

So on Next! library i am going to use prototyping on core types to make js more extended.

Its all for now, next ime i am going to talk about inherance and Class stuff


No comments: