$('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?就像:忽略下面的代码并继续下一个元素
Object.prototype.e = function() {
[].forEach.call(this, function(e) {
return e;
});
};
var w = [1,2];
w.e(); // undefined
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用警报,这是有效的
// ...
[].forEach.call(this, function(e) {
alert(e);
});
// ...
w.e(); // 1, 2
Run Code Online (Sandbox Code Playgroud)