<HTML>
<body>
</body>
<script>
function work(a,b){
alert(a + b);
}
function spy(func){
function wrapper (...args) {
//to make parameters to be each array by every call
wrapper.calls.push(args);
//this is call forwarding : wrapper passes everything it gets
//and return back its result
return func.apply(this, arguments);
//arguments is reserved word,
// means all argument the outter object has got
}//function definition can be anywhere in the block
wrapper.calls = []; //calls is an array
return wrapper;
}
work = spy(work);
//now work has the body of wrapper
//which has an array calls, and does push everytime it runs
work(1, 2); // 3
work(4, 5); // 9
for (let args of work.calls) {
alert( 'call:' + args.join() ); // "call:1,2", "call:4,5"
}
</script>
</HTML>
<!--
func.call(context, ...args) //spread operater(...) allows
to pass ITERABLE (can iterate but no index) as the likst to call
ITERABLE
func.apply(context, args) // it only accepts arraylike args
ARRAYLIKE
DIFFICULT
-->
<body>
</body>
<script>
function work(a,b){
alert(a + b);
}
function spy(func){
function wrapper (...args) {
//to make parameters to be each array by every call
wrapper.calls.push(args);
//this is call forwarding : wrapper passes everything it gets
//and return back its result
return func.apply(this, arguments);
//arguments is reserved word,
// means all argument the outter object has got
}//function definition can be anywhere in the block
wrapper.calls = []; //calls is an array
return wrapper;
}
work = spy(work);
//now work has the body of wrapper
//which has an array calls, and does push everytime it runs
work(1, 2); // 3
work(4, 5); // 9
for (let args of work.calls) {
alert( 'call:' + args.join() ); // "call:1,2", "call:4,5"
}
</script>
</HTML>
<!--
func.call(context, ...args) //spread operater(...) allows
to pass ITERABLE (can iterate but no index) as the likst to call
ITERABLE
func.apply(context, args) // it only accepts arraylike args
ARRAYLIKE
DIFFICULT
-->
댓글
댓글 쓰기