在我下面的 JS 代码中,我得到了单个控制台的 2 个答案,第一个是预期答案,另一个是undefined
.
我不明白为什么要获得第二名undefined
,谁能告诉我原因。
function sayHello2(name) {
var text = 'Hello ' + name;
var say = function() {
console.log(text);
}
return say;
}
var say2 = new sayHello2('Bob');
console.log(say2());
你没有从内部函数返回任何东西。如果没有从函数undefined
返回任何内容,则默认返回。你必须text
从内部函数返回:
return text;
function sayHello2(name) {
var text = 'Hello ' + name;
var say = function()
{
console.log(text);
return text;
}
return say;
}
var say2 = new sayHello2('Bob');
console.log(say2());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句