Ararat Harutyunyan Ararat Harutyunyan - 6 months ago 32
Javascript Question

Context binding syntax

Lets assume we have

function () {}.bind(null);


(function () {}).bind(null);

Why the first one doesn't work in developer console (it throws syntax error) but both of them work in code (".js" file in website).


I think its because you are trying to define an anonymous function. Console has no future way of referencing it so its a pointless endeavour, youre defining a function you have no way of calling. If you define it as a variable it works:

 var a = function () {}.bind(null); 

Also I dont think its binding related. Defining an anonymous function without binding and you have the same error


A statement that begins with the keyword "function" must be a valid function declaration statement. That requires a name for the function.

In an expression (or expression statement), that rule is different; no name is necessary because the function acts as a value in that context. No name is required then.

A function declaration cannot be anonymous, but a function expression can. A stand alone anonymous function looks like a function declaration that is missing an identifier to JavaScript. But, combined with an operator, JavaScript treats an anonymous function as the operator's operand expression.