Home › JavaScript Tutorial › JS advanced functions
JS advanced functions
Closures, rest, spread, bind.
A closure keeps access to outer variables. Rest (...args) collects arguments, spread expands them. bind fixes this.
Example code
<pre id='o'></pre>
<script>
function counter(){let n=0;return()=>++n;}
const c=counter();c();c();
const sum=(...a)=>a.reduce((s,x)=>s+x,0);
const hi=function(g){return g+', '+this.name;}.bind({name:'Ada'});
o.textContent=[c(),sum(...[1,2,3]),hi('Hello'),(function(a,b=10){return a+b})(1)].join('\n');
</script>