JS Generics
With JavaScript 1.6 Mozilla introduced generic version of a lot of the Array and String methods. Without broad support these very useful methods are in reality only available to Firefox and Firefox extension. Personally I’ve become too used to them to survive long without them. I’ve previously posted the JS 1.5 array extras and I’ve also mentioned these methods but now I’ve implemented them for JS1.3 (JScript 5.6 and whatever they call the JS version they use in Opera)
var arrayLike = {0: 3, 1: 2, 2: 1, 3: 0, length: 4};
Array.sort(arrayLike);
document.write(Array.join(arrayLike, "<br>"));
February 28th, 2006 at 0:57
Makes me wonder, will something like this make it natively in Opera 9? Either way, nice job!
February 28th, 2006 at 12:21
Nice one, will come in handy!
March 1st, 2006 at 5:04
Wow. Cool. Could we include your generics implementation in our open-source (LGPL) toolkit qooxdoo (http://qooxdoo.oss.schlund.de)?
Could please explain one thing I have found there:
constr[methods[j]] = function (constr, name)
{
return function (s) {
var args = Array.prototype.slice.call(arguments, 1);
return constr.prototype[name].apply(s, args);
};
}(constr, name);
What do the last line in this case? You define a function, but for what do you append this “(constr, name)”? Thank you for your help.
March 2nd, 2006 at 0:37
I have now licensed the file using Apache Software License 2.0. This is compatible with GPL so you can use it in qooxdoo.
The ugly syntax is because constr and name changes during the loop and the extra function actually binds the values to the right values (if this was not done all the methods would do this:
function (s) {
var args = Array.prototype.slice.call(arguments, 1);
return Stringprototype["slice"].apply(s, args);
}
not what one wants
March 3rd, 2006 at 3:39
Elegant solution! This is javascript the way I like it!
March 16th, 2006 at 2:53
The quote method has a bug: you have to replace \ with \\
return ‘”‘ + this.replace(/\\/g, “\\\\”).replace(/\”/g, “\\\”") + ‘”‘;
The quirk code can be made more readable in this way:
function extend(constr, name) {
return function (s) {
var args = Array.prototype.slice.call(arguments, 1);
return constr.prototype[name].apply(s, args);
};
}
if (!constr[name]) { constr[name] = extend(constr, name); }
March 17th, 2006 at 3:03
Vincenzo: True and true
I’ve now updated the script.
March 18th, 2006 at 22:05
An Interview with Dean Edwards…
Dean Edwards was kind enough to answer a few questions on JavaScript. Tell me a bit of your background and how you got into JavaScript programming? I’ve been a programmer (professionally) for twenty years. Most of that time I’ve been……
March 21st, 2006 at 6:50
apply( ) method is implemented in JavaScript 1.2, but the call( ) method is not implemented until JavaScript 1.5. according to the book “JavaScript: The Definitive Guide, 4th Edition”
March 21st, 2006 at 10:33
Call is part of ECMAScript edition 3 and all dialects of JS supports that (or otherwise they are not even worth using).
March 21st, 2006 at 22:12
Hi,
I’m not as advanced as you are but I am seeing this page with Opera and it throws these errors:
Inline script thread
Error:
name: TypeError
message: Statement on line 128: Expression evaluated to null or undefined and is not convertible to Object: xmlHttp
Backtrace:
Line 128 of linked script http://erik.eae.net/playground/newpost/newpost.js
xmlHttp.onreadystatechange = function ()
{
if (xmlHttp.readyState == 4)
{
self.onLoad();
}
}
;
Line 105 of linked script http://erik.eae.net/playground/newpost/newpost.js
this.reload();
Line 98 of linked script http://erik.eae.net/playground/newpost/newpost.js
this.setUri(sUri);
Line 2 of inline#1 script in http://erik.eae.net/archives/2006/02/28/00.39.52/
new NewPostChecker(”/playground/newpost/”);
Inline script thread
Error:
name: TypeError
message: Statement on line 33: Expression evaluated to null or undefined and is not convertible to Object: xmlHttp
Backtrace:
Line 33 of linked script http://erik.eae.net/playground/tunage/tunage.js
xmlHttp.onreadystatechange = function ()
{
if (xmlHttp.readyState == 4)
{
self.onLoad();
}
}
;
Line 10 of linked script http://erik.eae.net/playground/tunage/tunage.js
this.reload();
Line 28 of linked script http://erik.eae.net/playground/tunage/tunage-text.js
t.setUri(”http://erik.eae.net/playground/tunage/?action=currentlyplaying”);
Line 1 of inline#2 script in http://erik.eae.net/archives/2006/02/28/00.39.52/
bindTunageToElement(”tunage-text”);
Shouldn’t there be a way to avoid having these errors show in the javascript console?
I may be a bit offbeat…
April 26th, 2006 at 18:34
[...] Bisna grows so much that it’s not unrecognizable. A large set of JavaScript objects were created, some code is not my own creation, I can tell you; but the result is really impressive. Bisna pretends to be a new PHP framework in the future (and if I have enough time). Now it’s only intended to be the replacer for pAjax. So, it’s time to show you how the new code will look like: [...]
August 19th, 2007 at 16:05
[...] would be much cooler if we could use those techniques in all modern browsers. Luckily Erik Arvidsson already gave us his version of JS Generics back in 2006(!) I also found a demonstration of crossbrowser generics in Dan Webb’s Scripting [...]