谁能告诉我为什么下面的代码会产生我期望的警报框(RyeTofuRye),但随后会产生第二个带有“未定义”消息的警报框?至少那是它在我的 Windows 7 机器上的最新 FireFox 中所做的。
function makeSandwich(bread, meat) {
alert(bread + meat + bread);
}
var mySandwich = makeSandwich('Rye', 'Tofu');
alert(mySandwich);
谁能告诉我为什么下面的代码会产生我期望的警报框(RyeTofuRye),但随后会产生第二个带有“未定义”消息的警报框?至少那是它在我的 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
因为您不从 中返回值,makeSandwich()
所以.mySandwich
undefined
将其更改为:
function makeSandwich(bread, meat) {
return(bread + meat + bread);
}
var mySandwich = makeSandwich('Rye', 'Tofu');
alert(mySandwich);
makeSandwich
什么都不返回。将其更改为:
function makeSandwich(bread, meat) {
return(bread + meat + bread);
}