Problem with 'this' in function.call() in JavaScript
Posted: 10 Nov 2020, 08:09
Why the function.call() behaves differently with and without 'this', in JavaScript?
The result with 'this' in test.call() is same when 'this' is replaced by 'undefined'.
But without 'this' in test.call(), the result is NaN.
The result with 'this' in test.call() is same when 'this' is replaced by 'undefined'.
Code: Select all
function test(a,b){
console.log(a+b);
}
let args = [1,2]
test.call(this, ...args); // Output: 3
test.call(undefined, ...args); // Output: 3
But without 'this' in test.call(), the result is NaN.
Code: Select all
function test(a,b){
console.log(a+b);
}
let args = [1,2]
test.call(...args); // Output: NaN