JavaScript中fn()和fn.call()之间的区别

时间:2016-03-16 18:38:28

标签: javascript call

var tempFn = function(someText){
console.log(someText);
}

tempFn('siva');
// where I simply call the function with text 'siva'

VS

tempFn.call(this,'siva');
// where I call the function using call method

这些方法有什么区别?

1 个答案:

答案 0 :(得分:3)

当您使用call表单时,您明确了将调用该函数的上下文。

上下文将确定函数执行时this的值是什么。

在你的情况下,你传递this无论如何都是默认的,所以这是一个无操作。此外,您的tempFn函数不会调用this关键字,因此如果您传入不同的范围,无论如何都无关紧要。