面试题 005 - xmfenbei/study-javascript GitHub Wiki

问题:

修复面试题 004,让最后一个console.log()打印输出 Aurelio De Rosa.

答案:

console.log(test.call(obj.prop));

解析:

这个问题可以通过运用call()或者apply()方法强制转换上下文环境。如果你不了解这两个方法及它们的区别,我建议你看看这篇文章What’s the difference between function.call and function.apply?. 区别在传参方式:数组还是一个个传。

function.call(thisArg[, argument1[, argument2[, ...]]]);
function.apply(thisArg[, argumentArray]);