Posts

Showing posts with the label arguments

About JavaScript apply arguments limit

Just a quick one from ECMAScript ml ... it is true that browsers may have a limited number of arguments per function. This may be actually a problem, specially when we use apply to invoke a generic function that accepts arbitrary number of arguments. String.fromCharCode This is a classic example that could fail with truly big collection of char codes and here my suggestion to avoid such limit: var fromCharCode = (function ($fromCharCode, MAX_LENGTH) { // (C) WebReflection - DO THE FUCK YOU WANT LICENSE return function fromCharCode(code) { typeof code == "number" && (code = [code]); for (var result = [], i = 0, length = code.length; i ) { result.push($fromCharCode.apply(null, code.slice(i, i + MAX_LENGTH))); } return result.join(""); }; }(String.fromCharCode, 2048)); // example alert(fromCharCode(80)); // P alert(fromCharCode([80, 81, 82, 83, 84])...

setTimeout and setInterval with extra arguments ... once again!

Funny discussion today on twitter about " why on Earth IE still does not support extra arguments with setTimeout and setInterval " ... oh, well ... The execScript Behaviour Somebody in IE team thinks that the rest of the world should avoid extra arguments because of a bloody edge case as the third argument in IE is: // ... seriously ... setTimeout("Msgbox('WTF')", 0, "VBScript"); What IE Users Could Do Well, rather than create a closure every bloody time we would like to reuse a function with different arguments, something posible 10 years ago via ActionScript 1, every web developer (and not only) misses the opportunity to avoid closures using a de-facto standard for some unknown reason not part yet of ECMAScript specifications. For those interested I will show an example later, right now let's think about a solution compatible with VBScript for those mental developers, as I have been, brave enough to still use this language for some purpose. ...

arguments, callee, call, and apply performances

We have dozens of best practices to improve performances. We also have common practices to accomplish daily tasks. This post is about the most used JavaScript ArrayLike Object, aka arguments , and its performances impact over basic tasks. Why arguments While it's natural for JavaScripters to use such "magic" variable, as arguments is, in many other languages everybody knows it does not come for free and it is rarely used. <?php function myFunc() { // function call for each execution // rarely seen in good PHP scripts $arguments = func_get_args(); } ?> One clear advantage in PHP, Python, and many others, is the possibility to define a default value for each argument. <?php class UserManager extends MyDAL { public function exists($user='unknown', $pass='') { return $this->fetch('SELECT 1 FROM table WHERE user=? AND pass=?', $user, $pass); } } ?> This approach may brings automatically developers to code as ...

ES5 arguments and callee, I was wrong!

JavaScript is not JavaScript, I am not crazy, it is just a consideration between the language itself and what is behind it: another programming language with lower level rules and logic ... sounds silly and obvious, but please keep reading to understand what I mean. No Results Yet, But I've Already Lost My Battle I spread comments, I wrote post after posts to defend ECMAScript 5 arguments.callee decision with "use strict", but I have to admit I have never investigate the internal behavior of callee, an arguments property which is not what we think is ... Discovering In Core The Callee Property What I was thinking was something hilarious for a C or C++ programmer: an inherited property for a mutable instance. // JavaScript should have a "secret" Arguments class // and for each function, something like this function Test(){}; // we have declared the function Test // internally there should be a secret operation like this: Test._createArguments = function(args){...

Do Not Remove arguments.callee - Part II

Few days ago kangax demystified named function expressions , underling the importance of the read only function name property and how messy is Internet Explorer JScript engine with named function and their scope. Before that post, I wrote one about ECMAScript 5 decision to remove arguments.callee when "use strict"; is present (note in the link: it is not deprecated right now, it is an arguments property) How These Two Things Are Related While kangax post was more focused about possible leaks and unexpected functions definitions, I would like to grab more attention about what this problem will cause in tomorrow libraries and JavaScript usage. Removing arguments.callee, our code size, apparently faster in core thanks to this missed variable, will increase drastically and a big part of the beauty of JavaScript language will disappear with this callee decision. The Classic Configuration Object Problem How many libraries base constructors via configuration objects? jQuery Ajax ? ...

JavaScript arguments Weirdness!

As you know, arguments is a "magic undeclared variable" with a local scope present in each function body. This variable is an Object, with an Array like structure. checkArgs(1, 2, 3); function checkArgs(){ // even if we do not declare // arguments, this one will // be present with a length property // equal to 3 and respective // 1, 2, and 3 values at index // 0, 1, and 2 arguments.length; // 3 arguments[0]; // 1 Array.prototype.join.call(arguments,"-"); // will produce 1-2-3 }; Nothing new so far, but I bet not everybody knew some arguments "feature" which is browser dependent or somehow re-usable. The Common "For In" Behavior There is a particular behavior about arguments variable, it does not expose properties in a "for in" loop . checkArgs(1, 2, 3); function checkArgs(){ for(var key in arguments) alert(key); // nothing happened ... }; How can be possible? Simple enough, 0, 1, a...

[ECMAScript 5] Do Not Remove arguments.callee !

Being subscribed in dunno how many developers mailing list, I completely forgot to follow up the arguments and arguments.callee discussion. Accordingly to this John post , is with extreme surprise that I am discovering how critical is the gap between programming language developers and programming languages active users. Even if I read more than once piece of SpiderMonkey or Google Chrome, I am part of programming languages users and I can tell you for sure that the decision to remove arguments.callee from the language will be a complete mess. The Main Aim Of ECMAScript 5: Do Not Break What We Have So Far Unfortunately, Internet Explorer will be broken , because named function persists in the scope , causing naming conflicts everywhere ! setTimeout(function f(){ alert(window.f); }, 1000); Above code will alert the function f in every Internet Explorer . Moreover, as you can read in one comme of John's entry, Internet Explorer has function interpretation "problems" , d...

JavaScript bits and bops :-)

This post is about a couple of probably useful JavaScript functions, that on daily basis could make our code smarter ;) String.prototype.Replace Ok, ok, a prototype into a native constructor is not a good start point, but this one, strings dedicated, is probably one of those "must have" protos, a Replace with multiple inputs, as is for PHP. String.prototype.Replace = function(replace){return function(RegExp, String){ if(!(RegExp instanceof Array)) RegExp = [RegExp]; if(!(String instanceof Array)) String = [String]; for(var result = this, i = 0, l = 0, index = RegExp.length, length = String.length; i result = replace.call(result, RegExp[i], String[l return result; }}(String.prototype.replace); With above prototype it is possible to search and replace with multiple RegExps and multiple replacements. The proto works as native replace one, but it is possible to perform a replace like this as well: "abc".Replace([/a/, /b/, /c/], ...