我有以下内容:
function refreshGrid(entity) {
var store = window.localStorage;
var partitionKey;
...
...
如果满足“如果”条件,我想退出此功能。我该如何退出?我可以说休息、退出或返回吗?
我有以下内容:
function refreshGrid(entity) {
var store = window.localStorage;
var partitionKey;
...
...
如果满足“如果”条件,我想退出此功能。我该如何退出?我可以说休息、退出或返回吗?
if ( condition ) {
return;
}
return
退出返回的函数undefined
。
该exit
语句在 javascript 中不存在。
该break
语句允许您退出循环,而不是函数。例如:
var i = 0;
while ( i < 10 ) {
i++;
if ( i === 5 ) {
break;
}
}
这也适用于for
和switch
循环。
在要退出函数的任何地方使用 return 语句。
if(somecondtion)
return;
if(somecondtion)
return false;
您可以使用
return false;
或者return;
在你的情况下。
function refreshGrid(entity) {
var store = window.localStorage;
var partitionKey;
....
if(some_condition) {
return false;
}
}
如果满足就使用这个
做
return true;
您应该使用 return 如下:
function refreshGrid(entity) {
var store = window.localStorage;
var partitionKey;
if (exit) {
return;
}
我在 Google App Scripts 中遇到了同样的问题,并像其他人所说的那样解决了它,但还有更多......
function refreshGrid(entity) {
var store = window.localStorage;
var partitionKey;
if (condition) {
return Browser.msgBox("something");
}
}
这样,您不仅会退出函数,还会显示一条消息,说明它停止的原因。希望能帮助到你。