Reputation: 4028
Why is this allowed ?
var f = function() {
console.log(this.x);
}.bind({x:1})();
And why this is not or better why I get syntax error in this case ?
function f() {
console.log(this.x);
}.bind({x:1})();
So, why I need function expression syntax to get this work and is there a way to use bind
method directly on function declaration ?
Upvotes: 8
Views: 2555
Reputation: 57703
The second example works but the syntax is slightly off:
Surround the function in parens. I have to say that I'm not entirely sure why. It seems like it would work without the parens huh? :P
(function f() {
console.log(this.x);
}).bind({x:1})();
Upvotes: 5