There are a lot of different cases here. I'll try to address as many as I can think of, but others might spot a few additional ones.
For starts, return
is not a function. It's a keyword. So you're out of luck there. (Unless you can write your function in a single statement, in which case ES6 arrow functions make the return implicit, like f=s=>s.length
instead of f=s=>{return s.length;}
.)
For static-like functions such as setInterval
or Math.sin
you can just give them a new name:
t=setInterval
s=Math.sin
If you want to call a function on an object repeatedly, you'll have to work a bit harder though. For instance if you want to use s.charCodeAt
multiple times on a string s
, then you cannot do:
c=s.charCodeAt
c(1)
c(2)
...
Because if invoked like this, c
will not know it's this
-context. The simplest option here is to store c
as a property of s
:
s.c=s.charCodeAt
s.c(1)
s.c(2)
If you're using this very often though, you don't want to write the s.
every time. To get rid of that, you'll need to write your own function. Using ES6 notation:
c=i=>s.charCodeAt(i)
c(1)
c(2)
Note however that this requires that you always want to call it on the same s
. However, if you want to use a shortened member function on several objects, you can make those parameters:
c=(o,i)=>o.charCodeAt(i)
c(s,1)
c(t,2)
...
Then there's also the option to modify the prototype to patch the function into every instance of a class, but I'd have to look up the details.
1Use jQuery library ? – Michael M. – 2014-10-21T14:21:26.263
1Although tips in languages are encouraged here, this is a very particular question that might not entertain more than 1 or 2 answers. It would get better reception on SO :) – Optimizer – 2014-10-21T14:22:04.207
Having said that, ES6 does not help in reducing load from defined functions when using just once. – Optimizer – 2014-10-21T14:22:31.987
5@Optimizer What's the significance of how many answers an advice question might get? – Martin Ender – 2014-10-21T14:29:47.420
5FYI:
return
is not a function. – Ingo Bürk – 2014-10-21T14:33:45.6401I'm aware of that - I acutally meant "long" strings in general :) – misantronic – 2014-10-21T16:05:26.013