0

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.

3
  • Wrap it in a try{}catch{}, if you need to end just do something like "undefined_var / undefined" ;p Commented Sep 17, 2011 at 20:15
  • So, what is the problem, exactly? Have you tried something? Commented Sep 17, 2011 at 20:16
  • Go for readability instead of compactness. You can always minify your code with tools (and they will do a better job). Commented Sep 17, 2011 at 20:17

5 Answers 5

1

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

Under what condition will this execute the return? True or False?
aFunction would return immediately when checkFunction() returns false. If you'd prefer it to return immediately when checkFunction() returns true, just remove the !
It's fetching the boolean value from an attribute I created on an element. Something like this: <div locked="true">-content-</div> will it read the attribute value as a string or a boolean value? The reason I'm asking is because it's executing the code regardless if it's true or false.
You know what, the value it's returning is null from the custom attribute. There's something wrong with my code. I'm using the getAttribute() method... hang on.
0

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
}

Comments

0

I would simply do this:

function checkFunction(){
  return (condition);
} 

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

2 Comments

Under what condition will this method execute the return, true or false?
@Felipe: When checkFunction() returns a value which evaluates to false.
0

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
    }
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.