有没有办法在JavaScript中使用其他功能结束功能?

发布于 2024-12-05 06:36:37 字数 343 浏览 3 评论 0原文

我想要一个函数在调用特定函数之前检查条件是否为真运行。我的目标是在另一个函数中包含一行代码(被调用的函数)。该函数应在执行任何其他代码之前运行。这里有一些伪代码来演示我的意思:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浮生未歇 2024-12-12 06:36:37

没有什么是专门为你想要的东西而设计的,而且无论如何它都接近于糟糕的做法。但你可以通过编写类似以下内容来使其变得非常简洁:

function aFunction()
{
   if (!checkFunction()) return;
   //some code
}

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:

function aFunction()
{
   if (!checkFunction()) return;
   //some code
}
靑春怀旧 2024-12-12 06:36:37

您可能需要一个sustert类似的函数,但另一方面则是:

function stopIfTrue(x) {
    if(x === true) throw "stop function"; // can be any string
}

然后:

function aFunction(){
    stopIfTrue(something); // if 'something' is true an error will be thrown; function will exit
    //some code
}

You might want an assert-like function, but then the other way round:

function stopIfTrue(x) {
    if(x === true) throw "stop function"; // can be any string
}

and then:

function aFunction(){
    stopIfTrue(something); // if 'something' is true an error will be thrown; function will exit
    //some code
}
朮生 2024-12-12 06:36:37

我会简单地这样做:

function checkFunction(){
  return (condition);
} 

function aFunction(){
    if(!checkFunction()) return;
    //some code
}

I would simply do this:

function checkFunction(){
  return (condition);
} 

function aFunction(){
    if(!checkFunction()) return;
    //some code
}
﹏半生如梦愿梦如真 2024-12-12 06:36:37

如果您想要做的是:

function aFunction()
{
    if(checkFunction())
    {
        return;
    }
    //somecode
} 

在 aFunction() 中不使用 return,您可以这样做:

function aFunction()
{
    if(!checkFunction())
    {
        //somecode
    }
}

If what you're trying to do is this:

function aFunction()
{
    if(checkFunction())
    {
        return;
    }
    //somecode
} 

without using a return in aFunction(), you could do this:

function aFunction()
{
    if(!checkFunction())
    {
        //somecode
    }
}
如梦 2024-12-12 06:36:37

您可以做的一个技巧是动态更改其他函数。所以

function make_wrapped(before, after){
    return function(){
        if(before()) return;
        after.apply(this, arguments);
    }
}

//set aFunction to be the new function
aFunction = make_wrapped(checkFunction, aFunction);

编辑:我读错了这个问题。这可能比您需要的要复杂得多。

A trick you can do is dynamically change the other function. So

function make_wrapped(before, after){
    return function(){
        if(before()) return;
        after.apply(this, arguments);
    }
}

//set aFunction to be the new function
aFunction = make_wrapped(checkFunction, aFunction);

edit: I misread the question. This is probably way more complicated than you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文