0

谁能告诉我为什么下面的代码会产生我期望的警报框(RyeTofuRye),但随后会产生第二个带有“未定义”消息的警报框?至少那是它在我的 Windows 7 机器上的最新 FireFox 中所做的。

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

4 回答 4

2

当您返回 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
于 2012-06-27T01:22:36.957 回答
0

因为您不从 中返回值,makeSandwich()所以.mySandwichundefined

于 2012-06-27T01:20:30.473 回答
0

将其更改为:

function makeSandwich(bread, meat) {
return(bread + meat + bread);
}
var mySandwich = makeSandwich('Rye', 'Tofu');
alert(mySandwich);
于 2012-06-27T01:20:49.170 回答
0

makeSandwich什么都不返回。将其更改为:

function makeSandwich(bread, meat) {
    return(bread + meat + bread);
}
于 2012-06-27T01:20:55.693 回答