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.
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With