第一個寫法會出現 ReferenceError 的原因,是因為 javascript 無法找到 gy 屬性的 base value。但如果直接指名 base value 為 window,即使 window 物件沒有 gy 屬性,還是不會出現 ReferenceError。
詳細請參考這篇:http://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/
To summarize, an anti-pattern is a bad design that is worthy of documenting. Examples of anti-patterns in JavaScript are the following:
Polluting the namespace by defining a large number of variables in the global context
Passing strings rather than functions to either setTimeout or setInterval as this triggers the use of eval() internally.
Prototyping against the Object object (this is a particularly bad anti-pattern)
Using JavaScript in an inline form as this is inflexible
The use of document.write where native DOM alternatives such as document.createElement are more appropriate.
Essential JavaScript Design Patterns For Beginners