Posts

Showing posts with the label apply

bind, apply, and call trap

quick one out of ECMAScript ml var // used to trap function calls via bind invoke = Function.call, // normal use cases bind = invoke.bind(invoke.bind), apply = bind(invoke, invoke.apply), call = bind(invoke, invoke) ; What Is It This is a way to trap native functions method in a handy way. Used in a private scope, it can address these methods once so that we can rely nobody can possibly change them out there for some script injection and only if we are sure the script has been loaded at the very beginning. How To Use Them Here few examples: // secure hasOwnProperty var hasOwnProperty = bind(invoke, {}.hasOwnProperty); // later on hasOwnProperty({key:1}, "key"); // true hasOwnProperty({}, "key"); // false // direct slice var slice = bind(invoke, [].slice); slice([1,2,3], 1); // 2,3 slice(arguments); // array // direct call call([].slice, [1,2,3], 1); // 2,3 // direct apply apply([].slice, [1,2,3], [1]); // 2,3 // bound method var o = {name:...

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])...

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 ...

PHP - apply, call, and Callback class

The "news" is that while some developer is putting a lot of effort to emulate PHP functionalities with JavaScript, I would like to have JavaScript functionalities in PHP. Nested functions, closures, and injected scope, are only some of JS cool stuff that's currently missing PHP (hoping that during this summer of code someone will implement at least one of them). Today what I'm going to emulate is a partial implementation of apply and call Function.prototype methods, and this is the basic example: function apply($name, array $arguments = array()){ return call_user_func_array($name, $arguments); } function call(){ $arguments = func_get_args(); return call_user_func_array(array_shift($arguments), $arguments); } As is for JavaScript, the main difference between these functions is that apply accept an array of arguments while call accepts an arbitrary number of arguments. echo call('md5', 'hello world'); // 5eb63bbbe01eeed093cb22bb8f5acdc3 ec...

How to inject protected methods in JavaScript - Part II

As a performances maniac, I've found another way to inject protected methods, or something similar, without unnecessary overload of each public method. Of course, precedent way is definitively more clear, linear, but if you have a constructor.prototype with a lot of private methods, the overload process will be a wall in front of method execution speed. Each time you will use a public method, you have to set, and unset, every protected method, and at the same time, as explained in my precedent post, you cannot send the object outside during public method execution, because it will expose every method, protected included. This new proposal is based on a Function like strategy, applied to instances: apply, and call. Please have a look at this prototype function: function prototype(constructor, public, protected){ // webreflection - mit style if(protected){ public.apply = function(callback, arguments){ return (callback.charAt(0) === "_" ? protected : this)[callback]....