Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest way of creating a "new function" alias in javascript

What's the shortest way (characters) of creating a "new function" alias.

Basically this is for code golf and minifying code beyond reason. So when you usually would write:

a=function(a,b,c){return a+b+c;}

You could write something like (also let's abstract return keyword as well with global variable R):

a=$("a,b,c","R=a+b+c")
a=$(a,b,c){R=a+b+c}

(Not sure if the second one is possible.)

For the first example the best I've come up with is:

$=function(a,b){return new Function(a,"R=0;"+b+";return R")}

Both the sizes (usage, declaration) matter but usage size is more important.

like image 897
Egon Avatar asked Aug 07 '10 18:08

Egon


People also ask

What is the syntax for creating a function in JavaScript name as func?

JavaScript Function Syntax A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().

What doe => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

What is new function in JavaScript?

New keyword in JavaScript is used to create an instance of an object that has a constructor function. On calling the constructor function with 'new' operator, the following actions are taken: A new empty object is created.

Where should we place use strict?

The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables. The numbers in the table specify the first browser version that fully supports the directive. You can use strict mode in all your programs.


1 Answers

I don't think new Function() is of viable use for most functions even without performance concerns because unlike function() {} no closure is created. The compiled function will only have access to its own local scope and the global object. Think about that for a second. Javascript without closures is like Java without classes or C without pointers. Clearly everything would break.

Anyways, if you only intend to use this alias for short lambada like expressions that don't need clousers, one obvious way to make things even more terse is to leave off the parameters deceleration. Simply assume that a = arguments[0]; b = arguments[1]; etc...

$=function(b){return new Function('a,b,c,d,e,f,g,h,i,j', b);};

Another way would be to automatically return the value of the last expression, instead of needing it to be explicitly declared

$=function(body) {
  return function(a,b,c,d,e,f,g,h,i,j) { return eval(body); };
};

Horrifying isn't it? This works because eval()...well it returns the value of the last evaluated expression. No return needed. Of course this method is an even bigger hit to performance, as the code in body is reevaluated each time the function is called, while with new Function the code is (or should be) only compiled once.

Anyways performance be dammed, with the previous method your function declaration is down to this

var myeyes = $('a+b+c');
alert(myeyes(1,2,3)); //6

Purdy huh? Still, I think it would look better like this

'a+b+c'

Looks like a string yes? Well...it is a sting, but in the hands of the right function, it could be evaluated as if it were a full on function literal

//Lets just assume that IE does not exist mmkay?
var funcs = ['map', 'filter', 'every', 'some'];
for (var i=0; i<funcs.length; i++) {
   (function() {
      //Store original function
      var name = funcs[i] 
      var _super = Array.prototype[name];
      Array.prototype[name] = function() {
        //$ !== jQuery
        if (typeof arguments[0] == 'string') arguments[0] = $(arguments[0]);

        return _super.apply(this, arguments);
      };
    }());
 }

Now you can write

[1,2,3,4,5].map('a*a');

instead of

[1,2,3,4,5].map(function(a){return a*a;});

which is even better than Firefox's Expression Closure

[1,2,3,4,5].map(function(a) a*a);

Try it out here: http://jsbin.com/iyogu3/edit

If only we could actually write function expressions like this without calling upon the eval monster. Really, one of my main gripes with Javascript syntax is the requirement to spam function(){} throughout your code. Some kind of shorthand function literal syntax (not the aforementioned half-assed expression closure) that was interpreted the same as a regular verbose function literal would go a long way to making my code look a little less ridiculous. And it might help minifying a tiny bit as well.

like image 184
MooGoo Avatar answered Nov 06 '22 19:11

MooGoo