The shortest way to create an alias for a "new function" in javascript

What is the shortest path (s) for creating an alias "new feature".

This is mainly for golf code and for code exclusion. Therefore, when you usually write:

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

You can write something like (also let the abstract keyword return also with the global variable R):

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

(Not sure if a second one is possible).

In the first example, the best I came up with is:

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

Dimension values ​​(usage, declaration) are important, but usage size is more important.

+5
source share
5 answers

, new Function() , , function() {} . . . Javascript Java C . , .

, - , , - . , a = arguments[0]; b = arguments[1]; ..

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

,

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

? , eval()... , . return. , , body , new Function ( ) .

,

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

, ? , , , .

'a+b+c'

? ... , , 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);
      };
    }());
 }

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

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

, Firefox Expression Closure

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

: http://jsbin.com/iyogu3/edit

, , eval monster. , Javascript function(){} . - ( ), , verbose, , . .

+18

:

function L(a,x){return new Function(a,'return '+x)}

:

n=L('a,b,c','a+b+c');
+1

, , . "@" "".

$=function(a,b){return new Function(a,b.replace(/@/g,"return "))}
a=$("a,b,c","@a+b+c")
0

- :

eval("t=Fa+b};t(1,2)".replace("F", "function(a,b){return "))
0
source

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

Short code: a=(a,b,c)=>a+b+c

The shortest function declaration possible in js is 4 characters

Example: _=>1

0
source

All Articles