有没有办法在JavaScript中使用其他功能结束功能?
我想要一个函数在调用特定函数之前检查条件是否为真运行。我的目标是在另一个函数中包含一行代码(被调用的函数)。该函数应在执行任何其他代码之前运行。这里有一些伪代码来演示我的意思:
function checkFunction(){
//checks if a condition is true if so end function, else continue function
}
function aFunction(){
checkFunction();
//some code
}
我知道我可以制作一个包含返回的条件语句,但如果可能的话,我想让它保持这么短。
谢谢你们抽出时间。
I would like to have a function that checks if a condition is true run before a specific function is called. My goal is to have one line of code (the function being called) in another function. This function should run before any other code is executed. Here is some pseudocode to demonstrate what I mean:
function checkFunction(){
//checks if a condition is true if so end function, else continue function
}
function aFunction(){
checkFunction();
//some code
}
I know I can make a conditional statement that contains a return, but I would like to keep it this short if possible.
Thank you guys for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有什么是专门为你想要的东西而设计的,而且无论如何它都接近于糟糕的做法。但你可以通过编写类似以下内容来使其变得非常简洁:
There's nothing designed specifically for what you want, and its verging on bad practice anyway. But you could get it pretty succinct by just writing something like:
您可能需要一个
sustert
类似的函数,但另一方面则是:然后:
You might want an
assert
-like function, but then the other way round:and then:
我会简单地这样做:
I would simply do this:
如果您想要做的是:
在 aFunction() 中不使用 return,您可以这样做:
If what you're trying to do is this:
without using a return in aFunction(), you could do this:
您可以做的一个技巧是动态更改其他函数。所以
编辑:我读错了这个问题。这可能比您需要的要复杂得多。
A trick you can do is dynamically change the other function. So
edit: I misread the question. This is probably way more complicated than you need.