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 ?
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})();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With