【发布时间】:2018-03-22 04:56:36
【问题描述】:
我在学习 Javascript 的过程中遇到了apply 函数。我想我可以将我的apply 值分配给一个变量,然后打印出变量的内容。但是,我似乎没有定义,我不确定为什么会这样......
var thisObj = {
fullName: 'test',
setName: function(firstName, lastName) {
this.fullName = firstName + " " + lastName
}
}
function getInput(firstName, lastName, callBack, createdObj) {
return callBack.apply(createdObj, [firstName, lastName]);
}
var thisObjectInstantiated = getInput("Michael", "Jackson", thisObj.setName, thisObj);
console.log(thisObjectInstantiated); // Why is this undefined?
我还注意到,如果我将打印更改为这样的事情,我的名字是正确定义的。
var thisObjectInstantiated = getInput("Michael", "Jackson", thisObj.setName, thisObj);
console.log(thisObj.fullName); // This is defined properly!
为什么我不能只将应用的结果存储在thisObjectInstantiated 变量中?谢谢。
【问题讨论】:
-
函数
setName不返回任何东西,所以它是undefined
标签: javascript apply