2

I was fooling around with JS a bit, and I found this:

enter image description here

Anyone care to explain?

4 Answers 4

6

An anonymous function is one that doesn't have a name. For example, you can do:

(function(){ alert("Hello World!") })();

This creates a function with no name and immediately calls it. If the code caused an exception to be raised, the JavaScript runtime will report a failure in an anonymous function.

Also, functions are themselves objects with a class named Function. You can use this class to define a new function like this (instead of the built-in syntax):

(new Function("x", "y", "return x+y"))(1, 2);

This is pretty much the same as writing:

(function(x, y) { return x + y })(1, 2);

This gives you a peek into the object-oriented nature of JavaScript's functions.

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

Comments

1

When you call the Function() function (which is a constructor of Function objects) it returns you a function. Functions created dynamically in that way have no name, and so the name "anonymous" is given to it.

See: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function

8 Comments

+1 - Functions bound to HTML elements are also anonymous. For example, if you had <a id="foo" onclick="window.alert(123);"> and looked at foo.onclick under a debugger, you'd get the same thing.
If I do Function('test'), I receive: function anonymous() { test }. This still returns function with name anonymous. Last argument is body of the function, all arguments before it are function arguments.
Your argument 'test' specifies the code body of the function. There is no (standard) way to set the name of the function as seen in the debugger.
Yea @Tadeck is correct - The Function constructor does not have a parameter for the name of the function. It always returns a reference to an anonymous function.
I wouldn't say the name "anonymous" is given to it. If it was, the following code wouldn't give a ReferenceError... Function('alert(anonymous)')(); It's displayed with that name, but the function doesn't actually have the name.
|
1

It is a quirk in the way multiple browsers' implementations of Function.prototype.toString renders functions created via the Function constructor, but it is not significant and it does not appear in any version of the EcmaScript specification.

Normally a named function

function anonymous(x) {
  if (x) {
    alert('hi');
  } else {
    anonymous(!x);
  }
}

will alert regardless of the value passed in, because the name of the function can be used to call it recursively (modulo IE bugs), but that is not the case with the anonymous created via new Function.

(new Function('x', 'if (x) alert("hi"); else anonymous(!x);'))(false)

fails with an error.

1 Comment

+1 for "It is a quirk...it is not significant and it does not appear in any version of the EcmaScript specification". Function(); anonymous(); // ReferenceError
0

An anonymous function is a function with no name. They aren't specific to Javascript, see http://en.wikipedia.org/wiki/Anonymous_function

For JS, basically instead of this:

function myFunc() { }

you can do:

var myFunc = function() { }

1 Comment

-1 Please look again into the question - you will see that the question is about function anonymous() { from the console - see the screenshot OP included.

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.