我有一个目标数组["apple","banana","orange"],我想检查其他数组是否包含任何一个目标数组元素.
例如:
["apple","grape"] //returns true;
["apple","banana","pineapple"] //returns true;
["grape", "pineapple"] //returns false;
Run Code Online (Sandbox Code Playgroud)
我怎么能在JavaScript中做到这一点?
$('button').click(function () {
[1, 2, 3, 4, 5].forEach(function (n) {
if (n == 3) {
// it should break out here and doesn't alert anything after
return false
}
alert(n)
})
})Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click me</button>Run Code Online (Sandbox Code Playgroud)
我的问题:虽然我打电话,为什么它仍会提醒下一个号码return?就像:忽略下面的代码并继续下一个元素
通过阅读本论坛中已经提到的与上述主题相关的所有问题(参见标题),我完全理解finally总是被称为.(除了System.exit和无限循环).但是,我想知道是否return在catch块中调用了a,然后return从finally块调用了另一个.
例如:
public static void main(String[]args) {
int a = new TestClass().absorbeTheValue();
}
int absorbeTheValue() {
try {
int a = 10/0;
if (a > 0) return 4;
} catch(Exception e) {
return 45;
} finally {
return 34;
}
}
Run Code Online (Sandbox Code Playgroud)
所以这里输出(当调用方法时)在任何情况下都是34.这意味着终于总能运行.我认为虽然其他"回归"根本没有运行.在很多帖子中,我发现最后将内容写入catch子句返回已写入的内容.我的理解是,一旦catch子句中的返回值即将被评估,控制流就会传递给finally子句,而子句又有另一个返回,这次返回将被评估,而不会将控制权传递给catch子句.通过这种方式,return在运行时唯一被调用的是最终返回.你同意吗?
甲return在finally不传递回控制到程序但返回值,并结束方法.我们可以这样说吗?