What have you found for these years?

2011-02-15

javascript checker (2)

因為我很好奇為什麼 closure-linter 有這麼多詭異的規則,
所以看了一下他們的 Google JavaScript Style Guide.
結果該說我果然太天真了嗎?javascript 真的是病得比我想像的
要來得嚴重得多耶。

在這一條中: Always use semicolons. 可以找到一個恐怖的範例:
(我調整了一下,以便可以直接透過 v8 執行)
// 1.
var myMethod = function() {
  return 42;
}  // No semicolon here.

(function() {
  // Some initialization code wrapped in a function to create a scope for locals.
})();
總覺得這應該沒問題才對,結果跑了:
v8 test.js
還真的給我跳出錯誤:
test.js:7: TypeError: number is not a function
})();
 ^
TypeError: number is not a function
    at Number.CALL_NON_FUNCTION (native)
    at test.js:7:2
加了 semicolon 後果然也就可以了。稍微試了一下,如果改成
以下的話其實也可以:
var myMethod = function() {
  return 42;
}  // No semicolon here.

var f = function() {
  // Some initialization code wrapped in a function to create a scope for locals.
};
semicolon 如果加在中間的話也可以:
var myMethod = function() {
  return 42;
}  // No semicolon here.

;

(function() {
  // Some initialization code wrapped in a function to create a scope for locals.
})();
中間加入 var v = 1 不行:
var myMethod = function() {
  return 42;
}  // No semicolon here.

var v = 1

(function() {
  // Some initialization code wrapped in a function to create a scope for locals.
})();
會顯示:
test.js:5: TypeError: number is not a function
var v = 1
        ^
TypeError: number is not a function
    at Number.CALL_NON_FUNCTION (native)
    at test.js:5:9
但那個 var v = 1 有加 semicolon 就可以。拿掉下面的話也行:
var myMethod = function() {
  return 42;
}  // No semicolon here.

var v = 1
簡單地說,我想他的 semicolon inference 的 algorithm is broken,
and that's why we should always use semicolons...

到底是誰覺得 javascript 好啊?這根本只能用好恐怖來形容嘛。

4 retries:

Poga Po said...

所以才會有Javascript: the good part這本書被奉為經典.. XD

Lin Jen-Shin (godfat) said...

棍就沒人想把那些該死的東西改掉嗎?
開個 server mode 嘛!這總沒有 compatibility 的問題了吧?
而且既然沒人會那樣寫程式,修好應該也不會弄壞什麼吧?
number is not a function 勒.....

yen3 said...

顯然幹意很深 XD

Lin Jen-Shin (godfat) said...

嗚嗚... XD

Post a Comment

Note: Only a member of this blog may post a comment.



All texts are licensed under CC Attribution 3.0