是否有一组东西是每个JavaScript程序员都应该知道的,以便能够说“我懂JavaScript”?


当前回答

jQuery。YUI。不是(等等等等)

框架可能很有用,但它们经常隐藏JavaScript和DOM实际工作的细节,这些细节有时很难看。如果你的目标是能够说“我懂JavaScript”,那么在一个框架上投入大量的时间是与此相反的。

这里有一些JavaScript语言的特性,你应该知道它在做什么,不会被发现,但对很多人来说不是很明显:

That object.prop and object['prop'] are the same thing (so can you please stop using eval, thanks); that object properties are always strings (even for arrays); what for...in is for (and what it isn't). Property-sniffing; what undefined is (and why it smells); why the seemingly-little-known in operator is beneficial and different from typeof/undefined checks; hasOwnProperty; the purpose of delete. That the Number datatype is really a float; the language-independent difficulties of using floats; avoiding the parseInt octal trap. Nested function scoping; the necessity of using var in the scope you want to avoid accidental globals; how scopes can be used for closures; the closure loop problem. How global variables and window properties collide; how global variables and document elements shouldn't collide but do in IE; the necessity of using var in global scope too to avoid this. How the function statement acts to ‘hoist’ a definition before code preceding it; the difference between function statements and function expressions; why named function expressions should not be used. How constructor functions, the prototype property and the new operator really work; methods of exploiting this to create the normal class/subclass/instance system you actually wanted; when you might want to use closure-based objects instead of prototyping. (Most JS tutorial material is absolutely terrible on this; it took me years to get it straight in my head.) How this is determined at call-time, not bound; how consequently method-passing doesn't work like you expect from other languages; how closures or Function#bind may be used to get around that. Other ECMAScript Fifth Edition features like indexOf, forEach and the functional-programming methods on Array; how to fix up older browsers to ensure you can use them; using them with inline anonymous function expressions to get compact, readable code. The flow of control between the browser and user code; synchronous and asynchronous execution; events that fire inside the flow of control (eg. focus) vs. events and timeouts that occur when control returns; how calling a supposedly-synchronous builtin like alert can end up causing potentially-disastrous re-entrancy. How cross-window scripting affects instanceof; how cross-window scripting affects the control flow across different documents; how postMessage will hopefully fix this.

请看关于最后两项的答案。

最重要的是,你应该批判性地看待JavaScript,承认由于历史原因,它是一种不完美的语言(甚至比大多数语言都不完美),并避免它最糟糕的故障点。Crockford在这方面的工作绝对值得一读(尽管我并不100%同意他关于“好的部分”是什么)。

其他回答

它可以被禁用。

在Javascript中,性能很重要。

没有智能编译器来优化你的代码,所以你应该更小心,当你写javascript代码比语言像c#, Java…

如果你想成为一个真正的JavaScript高手,你应该知道完美JavaScript测试中每个问题的答案。

下面这个例子可以让你胃口大开:

(function f(f){ 
  return typeof f(); 
})(function(){ return 1; });

这个表达式返回什么? “数量” “定义” “功能” 错误

真正学好一门语言并理解它的各种怪癖来自(多年的)经验。如果你想成为一个更好的程序员,我会说,了解设计模式,如何以及何时使用它们,甚至在你没有意识到的情况下使用它们;技术架构和用户体验。

Knowing the (JavaScript) language means you can pick up any framework and use it at will. You'll inevitably need to dive into the source code, and if all you know is the syntax a framework or 2 or 3, then you won't go far. In saying that, getting into a few different frameworks' source code is probably one of the best ways to see how JavaScript can be used. Messing about by stepping through the code in Firebug or Web Inspector, then checking JavaScript Documentation, especially the Mozilla and Webkit docs, to get further understanding of what you're looking at.

了解面向对象和函数式编程之间的区别,了解JavaScript是两者的完美结合,以及何时以及如何使用这两者来创建杀手级代码库和出色的应用程序,将使你成为更好的JavaScript程序员。

简单地阅读一些书籍,尤其是Crockford的“good parts”,它只展示了他对JavaScript中什么是好的观点,而跳过JavaScript的大部分“AWESOME”部分会让你一开始就走错了路。

另一方面,看看像Thomas Fuchs这样的人写的代码会让你更深入地了解编写令人惊叹和高效的JavaScript的力量。

试图记住一些陷阱或wtf也不会有多大帮助,如果你开始编码并逐步浏览一个库/框架的代码,特别是一个有帮助的注释的代码,你会发现为什么他们使用某些属性/值,而不是其他为什么以及什么时候使用特定的操作数和操作符是好的,这些都存在于框架的代码中。有什么比以身作则更好呢?: ^)

JSLint http://www.JSLint.com/