0

I am reading a book about javascript and I came across this syntax

this is a function

function unwantedTextEvent(){
  return (navigator.vendor == 'Apple Computer, Inc.' && (event.target == event.relatedTarget.parentNode
  || (event.eventPhase == 3 && event.target.parentNode == event.relatedTarget)));
};

and then inside another function , the author is doing just this

attachEventListener(li, 'mouseover', function(e){
 if (unwantedTextEvent()) { return; }
 clearTimeout(closetime);
 if (branch == li) { branch = null; }
 //and so on

Now, I am ashamed to admit that I have never seen that syntax again :

 if (unwantedTextEvent()) { return; }

and I dont have a clue what it does. Can anybody please explain to me? What does this syntax does in general?

Thanks in advance

4
  • 1
    It just calls the function and uses the returned value in the if condition. Commented Apr 15, 2014 at 18:54
  • Could you clarify which part of that code is confusing you? In general, the way to understand any code is to break it down into its constituent parts. If you understand what each piece does, the overall meaning should become clear. Commented Apr 15, 2014 at 18:59
  • It calls unwantedTextEvent() function and if return from this function is false then control returned from complete function(that anonymous function in attachEventListenter). Commented Apr 15, 2014 at 18:59
  • @Nish You're just repeating what all the answers say, aren't you? Commented Apr 15, 2014 at 19:00

5 Answers 5

1

That syntax calls a function called unwantedTextEvent(). If that function returns an affirmative Boolean value, then the callback function(e) inside of attachEventListener is returned.

It simply stops executing in the callback function.

Sign up to request clarification or add additional context in comments.

5 Comments

So if unwantedTextEvent() returns true, function(e) keeps executing and if unwantedTextEvent() returns false, function(e) stops? And code continues after the function(e)?
@user2860857 you have it flipped. If the condition is true(i.e. when unwantedTextEvent() is true), then function(e) returns. If it does not meet the condition, it goes on to check the rest of the function until it ends or returns due to another condition being met later.
OK, so look at this http://jsfiddle.net/slevin/fUeFk/12/. If I put a>4 (true), then only ping is alerted (function returns- gets executed untill it meets the if condition). If I put a<4 (false) then the whole function is executed. I think, now I got it...
@user2860857 there ya go :). Javascript has some cramped syntax that can be confusing at first.
OK, got it. Thanks. Funny thing, your answer confused my at first, but now, turns out its the most well-written. Thanks for your time
1

Basically, unwantedTextEvent() is just a big condition. If that condition is true, it stop running the function

The code after a return is never run.

It is the same as doing :

if (!unwantedTextEvent()) {
    clearTimeout(closetime);
    if (branch == li) { branch = null; }
    //and so on
}

4 Comments

So, if I got it right : if unwantedTextEvent() returns true, function(e) is not executed, and if it returns false, function(e) is executed? How can I edit the code to do like if returns false? Like if (unwantedTextEvent()) { !return; }
@user2860857 you got it. If you want the other way around, you'd have to negate the if : if (!unwantedTextEvent()) { return; }. See the band inside the if?
OK thanks. In general if its true, the function exits, if it is false the function continues. Am I right? Based on bwtrent's answer and this http://jsfiddle.net/slevin/fUeFk/12/
@user2860857 exactly!
0

Return exits the function immediately. That syntax exits without returning a value.

Comments

0

It's an ordinary if statement. The general form of if is:

if (<test expression>) {
    <code to execute>
}

optionally followed by an else clause (but your example doesn't have this).

In this case, <test expression> is a call to the function unwantedTextEvent. If it returns a true value, the <code to execute> is executed, and the function returns. Otherwise, it continues with the rest of the function.

4 Comments

That's not what the OP is asking.
@Scimonster - Looks like exactly what the OP is asking for ?
I'm usually pretty good at reading between the lines to figure out what people are asking, but I admit I'm not really sure which part of the code confused him.
It looked to me that he was confused about the return statement.
0

the return; statement simply exits out of the function passed into the attachEvenListener. i.e. the anonymous function.

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.