Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can function operator be aliased?

Tags:

javascript

Is there a way how to alias function operator without too much overhead like eval? I'd like to write

fn test() { ... }

instead of

function test() { ... }

to strip some bytes in minified code. Just curious.

like image 984
Jan Turoň Avatar asked Apr 29 '13 19:04

Jan Turoň


2 Answers

Is there a way to alias function operator without too much overhead?

Nope.

Unless of course you're using ECMAScript 6 which supposedly will contain what's called "fat arrow" syntax:

var test = (arg1, arg2) => arg1 + arg2;

Until then, you're stuck constantly declaring:

var test = function (arg1, arg2) { return arg1 + arg2 };

or

function test(arg1, arg2) {
    return arg1 + arg2;
}
like image 187
zzzzBov Avatar answered Nov 11 '22 11:11

zzzzBov


As Dave pointed out, it's best to write proper script and let gzip do its job.

But, if you're not afraid of eval() and being looked down on by your peers, but you could build a preprocessor to customize the language a bit -- one function of which can be enabling => declarations.

In some external file or hidden tag that needs pre-processing:

f=(x,y)=>{return x+y;}

In your preprocessor somewhere:

var s = loadCodeToPreprocess(whatever);
s = s.replace(/(\([^()]*\))=>/g, "function$1");
eval(s);

But again, it's usually best to just write JavaScript per the standard and let gzip do its job.

like image 44
svidgen Avatar answered Nov 11 '22 11:11

svidgen