2

Both Chrome and Safari report this is illegal. They report "unexpected token 'this'" pointing at the 'this.b'. Here is the minimum code needed to show the problem:

function x(){ this.a = function() {} this.b = function() {}  }

It only happens if the two declarations are on the same line. Any ideas? Looks legal to me.
It's annoying because this is what comes out of a Javascript minifier.

4

2 回答 2

2

Javascript 将自动在新行上插入分号。这里缺少的是用于分隔语句结尾的原始分号。

在你的右大括号之后插入分号,就像正确的 JS 一样,它会正常工作。

于 2013-04-05T20:36:02.520 回答
0

函数内部的第一条语句没有分号结束x

分号插入仅适用于新行。

// Valid but nasty
this.a = function() {}
this.b = function() {}

// Valid
this.a = function() {};
this.b = function() {};

// Valid
this.a = function() {}; this.b = function() {};
于 2013-04-05T20:36:04.647 回答