谁能告诉我下面的代码为什么生成我期望的警报框(RyeTofuRye),然后又生成另一个带有消息“ undefined”的警报框?至少这就是我的Windows 7计算机上最新的FireFox所做的事情。

function makeSandwich(bread, meat) {
alert(bread + meat + bread);
}
var mySandwich = makeSandwich('Rye', 'Tofu');
alert(mySandwich);

最佳答案

当您返回Nothing时,第二个警报未定义!
流程如下:

function makeSandwich(bread, meat) {//step-3
    alert(bread + meat + bread); // alerting RyeTofuRy, step-4
    // am i returnign anything? nope!
    //adding a return (bread + meat + bread); resolves teh problem though
    }

var mySandwich = makeSandwich('Rye', 'Tofu');//start here step-1
alert(mySandwich);//step-2, step-5

关于javascript - 成功调用带参数的简单函数后,出现消息“未定义”的意外警报,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11218125/

10-13 04:39