My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Friday, September 15, 2006

Why we need a JavaScript Standard Library

JavaScript 2.0 is on the air but in a lot of js oriented sites I can see every day show often the same, obvious or boring prototypes to normalize some old browser before every kind of different solution for every different problem.

Yesterday, for example, I've read about a "revolutionary" way to search a value inside an array, using an "horrible" Object solution.

If you read comments you could see that Array has a native, standard, prototype called indexOf that does exactly what that blog was talking about.

Well, why do you like to spend your time for these things ?

You like that because you don't know all JavaScript 1.6 objects methods or you don't use FireFox to develop your code and you don't have a low level code normalizer library.

However, one of the first comments shows the simplest solution for the problem, indexOf, but shows another Array.prototype normalizer function.

This is just one example of all posts that you can find on the net and everytime someone posts a Something.prototype normalizer solution for that problem, for that library.

Maybe in your 3rd part page scripts you have at least 2, 3 or more equal prototypes that normalize this, that or other library ... or recreate always the same result with a different name.

Don't you have enought ? Don't you care about sum of every script size ?

I wonder why JSL hasn't been linked from any "javascript specialist site" because to write a standard code, using for example JSLint, isn't enought to produce good, fast or optimized code.

I've talked about my JSL to Dojo developers too, to resolve a lot of common JS portability problems and to resolve encodeURIComponent or decodeURIComponent top-level function portability that are quite perfect only with JSL, as String.replace with function as second argument is.
Dojo developers didn't care about JSL ... they don't need another normalizer library (but they have a propetary normalizer library ... that doesn't make JS more standard for every other lib in a 100Kb of packed code lobrary ... ).

Prototype has its normalizer proto too but these aren't standard.
Array.each is not standard (while JS 1.6 has Array.forEach, that's a standard method), indexOf is not complete (and I havn't seen the String.indexOf implementation) ... then why rewr everytime the same normalizer prototype ?

Why increase every library with its normalizer prototype when JavaScript 1.6 has at least one of every normalizer proto used inside every library ?

Do you think this is a good way to depends everytime from each different library ?
Do you think your library is the "only cool one" and then every developer should learn a different (but same) implementation of every different library normalizer prototypes ?

We don't like IE because it doesn't respect standards, every JS library I've seen doesn't respect ECMA standards too.

Why don't you use JSL and then ECMA Standards for JS 1.6 ?

When finally all browsers will be compatible at least with JS 1.6, remove JSL will be a simple step while using prototype or other libraries dedicated implementation of "un-standard" ECMA will not be possible.

Don't you like this point of view ? Don't you like standards ?

Do you need some example ?


// original prototype versionfunction
$() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}

// JSL and prototypefunction
$() {
var elements = $A(arguments).forEach(function(element, i){
if(element.constructor === String)
elements[i] = document.getElementById(element);
});
return elements.length === 1 ? elements[0] :elements
}

// getElementsByClass from Top 10 JavaScript Function (Dustin Diaz)
function getElementsByClass(searchClass,node,tag) {
var classElements = new Array();
if ( node == null )
node = document;
if ( tag == null )
tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}

// JSL versionfunction getElementsByClass(searchClass,node,tag) {
var classElements = [],
els = (node || document).getElementsByTagName(tag || "*"),
pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
for(var i = 0, j = els.length; i < j; i++)
classElements.push(els[i]);
return classElements.filter(function(element){
return pattern.test(element.className)
})
}

// JSL and $A prototype versionfunction getElementsByClass(searchClass,node,tag) {
var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
return $A((node || document).getElementsByTagName(tag || "*")).filter
(function(element){return pattern.test(element.className)
})
}

// inArray from Top 10 JavaScript Function (Dustin Diaz)
Array.prototype.inArray = function (value) {
var i;
for (i=0; i < this.length; i++) {
if (this[i] === value) {
return true;
}
}
return false;
};

// unusefull inArray implementation with JSL
Array.prototype.inArray = function (value) {
return this.indexOf(value) !== -1
}

// maybe more usefull inArray (has) version with multiple arguments
Array.prototype.has = function() {
var i = arguments.length, result = [];
while(i)
result.push(this.indexOf(arguments[--i]) !== -1);
return result.every(function(e){return e})
};// ...


Finally, if a lot of developers think that top 10 JS funcs should be inside a common.js, why they don't think that a low-level lib as JSL is should be a must before every every top-ten common.js ?

It's a single file, as a common.js file should be, it's lightweight, it's to develop every kind of medium or high level lib or code using standards, it's a must to write code for FireFox and use it with every other browser .... then why don't you like my JavaScript Standard Library ?

7 comments:

Anonymous said...

/*
useless notation:
You aint bad. Okay, You're smart. But too much emotions. No need for tempting that much, noone likes it.
*/

The reason is simple as pie. People dont care. People use what is popular. JSL isnt popular - okay, noone use it. prototype is popular? okay, it's everywhere.

/* If you're bothered with my mootools references, dont read this: the same stuff is with mootools. It aint that popular because It's smart. As smart as JSL is. */

Smart things require smart usage. Prototype does not. Actually, I like your ideas without a doubt. They are obvious for us, who know what is the beauty of JS, who know what it worths.

While people don`t love JS as I do (and maybe you do) we wont have smart things popular.

Andrea Giammarchi said...

ooops, I forget another example, using standard string replace method, portable with IE4 too with JSL :P


// prototype version
String.prototype.camelize = function() {
var oStringList = this.split('-');
if (oStringList.length == 1) return oStringList[0];

var camelizedString = this.indexOf('-') == 0
? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1)
: oStringList[0];

for (var i = 1, len = oStringList.length; i < len; i++) {
var s = oStringList[i];
camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
}
return camelizedString;
}


// my version, naturally compatible with standard browsers,
// totally compatible with JSL
String.prototype.camelize = function(){
return this.replace(/\-(\w)?/g, function(a,b){return b.toUpperCase()});
};

Unknown said...

Hi Andrea,

In a way you're spot on with your remarks. On the other hand: I wrote such a thing for myself ages ago, and don't feel the need to replace it :). And somewhere I get the feeling most developpers with some common sense have done the same thing.

Why people don't use it is mainly due to not knowing it. After all, it hasn't got a good Google ranking (second page). Promote, promote, promote! You've got a nice blog here, but nowhere is JSL found on it. And the old site where it can be found is a pain to use => ssslllloooowwww loading man :D.

One other thing about promotion: I hate to say this, but I wrote you about JSL some time ago, concerning a defect implementation of Array.splice. Never heard of it again, never seen an eventual update.

If you want people to use your craftings, then don't dismiss the feedback you get. Not that I will loose any sleep on it, but still...

Don't get me wrong, JSL is a good idea and nicely done. Not my coding style, but that's just taste. I don't think the problem lies with JSL itself, but with the author's effort to promote it ;). If you work a bit on that, maybe people will start to catch up.

About the Array.splice: in short it comes down you missed some of the specifications in your version (using it with only one argument and some other details). I have some test files prepared to show it to you. If you're interested, mail at stefan dot van dot reeth at gmail dot com.

Andrea Giammarchi said...

stefan, I answered to your mail about 3 days ago ... and You didn't reply.

I'm waiting for your report (You tought about an example page to show me the error)

I'm still waiting and thank You for your feedback :-)

Unknown said...

Excuses then Andrea, but I never got the mail. Seems it lost it's way en route to Belgium (after all, who knows where to find us ;) )

Sending the test case to you this evening.

Andrea Giammarchi said...

Stefan, I've recieved your email, let me finish new JSL version, it will be included inside my new "all in one" library with a lot of fixes for JSL too (for example I've totally rewrote String.replace function and library core will be compatible with IE4 too, I suppose a totally cross-browser library on 2007 :D)

Please, stay tuned and regards :-)

Anonymous said...

Hi,

I've just discovered this and it seems useful. I have a bunch of questions: Is it still being maintained? Is it available from a source code repository (e.g. github)? What about an issue tracker? Is there a non-minified version available somewhere? Thanks!