Jay*_*ard -1 javascript jquery
当我准备一个函数时,我注意到它最初返回'undefined'以及它应该返回的其他信息.这是这个功能:
foo = function(bar) {
var glorp;
if(typeof bar == 'undefined'){bar = 'baz'}; // there will be other conditions later
if('baz' == bar) {
glorp += 'this, ';
glorp += 'that, ';
glorp += 'the other';
return glorp;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在调用这样的函数:
$('#glorp').append(foo());
Run Code Online (Sandbox Code Playgroud)
回报是这样的:
undefinedthis,那个,另一个
当我预料到这一点:
这个,那个,另一个
我做了很多挖掘,但我无法找到任何确定的东西.然后我将第一个glorp操作符更改为just =,未定义的消息消失了.
因为glorp在函数的开头if声明它,所以它应该在语句中定义,并且它似乎是因为'this,'被成功返回.
什么是返回'未定义'?
因为
var glorp; //<--undefined
console.log(glorp); //logs undefined
glorp = glorp + "x"; // undefined + "x" -> "undefined" + "x" -> "undefinedx"
console.log(glorp); //logs "undefinedx"
Run Code Online (Sandbox Code Playgroud)
将其设置为空字符串
var glorp = "";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |