Using the .prototype.bind Function Directly in a Function Declaration

Why is this allowed?

var f = function() { console.log(this.x); }.bind({x:1})(); 

And why is it not or better, why am I getting a syntax error in this case?

 function f() { console.log(this.x); }.bind({x:1})(); 

So, why do I need the function expression syntax to get this working, and is there a way to use the bind method directly in the function declaration ?

+7
javascript
source share
1 answer

The second example works, but the syntax is slightly off:

Surround a function in parens. I have to say that I do not quite understand why. Seems this will work without partners ?: P

 (function f() { console.log(this.x); }).bind({x:1})(); 
+3
source share

All Articles