从调用的函数内部中止外部函数
是否有可能让youth
从box
中返回/停止执行?直接,而不是类似:(
function youth(){
var check = true;
function box(){
//code
check = false;
}
//code
while(check){
//code;
}
return false;
}
其中有一个 check
变量,box
会发生变化)
Is it possible to make youth
return/stop executing from within box
? Directly, not something like:
function youth(){
var check = true;
function box(){
//code
check = false;
}
//code
while(check){
//code;
}
return false;
}
(where there is a check
variable which box
changes)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,你不能,除非你从内部函数抛出异常(但这并不能真正算作“返回”,而且无论如何它都不是一个好的设计模式)。
允许函数使其调用者返回一个值是没有意义的,因为调用函数甚至可能没有在相同的上下文中声明——也许它是接受函数参数的其他函数,并且该传入函数不应该能够修改被调用函数的行为。
No, you cannot, unless you throw an exception from the inner function (but that doesn't really count as "returning," and it's not really a good design pattern anyway).
It does not make sense to allow a function to make its caller return a value, since the calling function might not even be declared in the same context -- perhaps it is some other function that accepts a function argument, and that passed-in function shouldn't be able to modify the called function's behavior.