直接在函数声明中使用function.prototype.bind

问题描述:

为什么允许这样做?

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})();

那么,为什么我需要函数表达式语法来完成这项工作,有没有办法直接在函数声明上使用 bind 方法?

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:

将函数包含在parens中。我不得不说,我不完全确定为什么。似乎没有parens会工作吗? :P

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})();