Using function.prototype.bind directly on function declaration
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})();
链接地址: http://www.djcxy.com/p/25528.html
上一篇: Cobertura给参数列表太长