5

The following is a practice question for a Javascript exam followed by the answer.

I am confused because my initial reaction was to choose B. I understand that D is also correct, but when I am taking timed exams I will sometimes stop reading the rest of the answers if I find what I believe is a correct answer (depending on how confident I am it is correct). In this case, I was supremely confident that B was a correct answer.

I am still trying to figure out why I was wrong for picking B, and the "answer" only seems to confirm my choice. Am I taking crazy pills (i.e. how am I misreading this???)? Or is this just a mistake in the book I'm reading?

  1. Which of the following isn’t an attribute of an anonymous function?

    A. Anonymous functions can’t be called by any other code.
    B. Anonymous functions have a clearly defined name.
    C. Anonymous functions can be passed as parameters.
    D. Anonymous functions can’t be assigned to a DOM element declaratively
    

  1. Correct answer: D

    A. Incorrect: Anonymous functions can’t be called.
    B. Incorrect: Anonymous functions don’t have a name.
    C. Incorrect: Anonymous functions can be passed as parameters.
    D. Correct: Anonymous functions can’t be assigned to a DOM element declaratively.
    
15
  • 2
    @VLAZ because of scope that may not be accessible anywhere else, but in my understanding that's not an anonymous function since it has a name. By all means please share any evidence to the contrary if my idea of anonymous isn't entirely accurate though. Commented May 28, 2020 at 5:48
  • 1
    @VLAZ Just double checked, and the book says, verbatim, "As expected, an anonymous function has no name". Commented May 28, 2020 at 5:52
  • 3
    What does D even mean actually? Commented May 28, 2020 at 6:02
  • 2
    D makes zero sense (so it’s “correct”, but so is “Anonymous functions aren’t cute kittens that save our planet from giant alien robots”). You can’t assign functions to DOM elements. Assignments to DOM nodes fail. What is a declarative assignment? In the context of the book, it appears to refer to DOM events, and what it calls “Declarative event handling” is more widely known as HTML event attributes or inline event handlers (bad practice—use addEventListener instead). But you can use my myFunc example from above: it’s wrong as well! Commented May 28, 2020 at 8:02
  • 2
    @BVernon Sure, the example (with corrected syntax) onclick="function(){ alert('test'); }" doesn’t accomplish anything, but onclick="(function(){ alert('test'); })();" does. That’s calling an anonymous function. Your example fails, because the function is just not called. Try onclick="myNamedFunction" or onclick="function myNamedFunction(){ alert('test'); }" — two named functions that don’t accomplish anything in their context. So anonymous functions can absolutely be called within HTML event attributes. Commented May 28, 2020 at 8:11

1 Answer 1

3

You obviously picked the wrong book, I wouldn't dwell to much on it.

  • A: Anonymous functions can’t be called by any other code. not correct. Functions that cannot be invoked? What's the point?

  • B: Anonymous functions have a clearly defined name. Couldn't have been the correct answer because 'anonymous' and 'clearly defined name' are kind of opposites.

  • C: Anonymous functions can be passed as parameters. Seems to be the correct answer: function named(fn) { console.log(fn()) } Named function being invoked using an anonymous function as it's argument:

    named(function() { return 'anonymous response' }) will log anonymous response.

  • D can be true & not true depending on interpretation:

    • true: If you pass to an a tag onClick="" prop a function like this () => alert('This will not work!') it won't work;
    • false: It works if you pass to an a tag onClick prop a function like this (() => alert('This works!'))() (try running the bellow snippet inside an html file)

<a href="#" onclick="(() => alert('This works!'))()">Click Me</a>

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

2 Comments

Your answer to C contradicts the previous discussion in the comments, although I haven’t clarified that explicitly a year ago: a function expression or declaration has a parameter list, i.e. function ⟨parameter list⟩ ⟨function body⟩. When invoking the function, an argument list is passed to it. “Passing as parameters” isn’t really a thing. But even if the book meant arguments, then your example still passes an anonymous function. fn.name === "". An argument binding like fn isn’t a function name; but even if interpreted as such, you can remove fn and read arguments[0] instead.
@SebastianSimon I think he got confused about the question half way through. That is, the question asked which is wrong and not which is right. I think he forgot that. When I selected this as the answer, I think I must have overlooked that or I would have mentioned it. Nonetheless, if you take this into consideration, I believe he is otherwise correct.

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.