问题描述
每个JavaScript程序员都应该知道一组能够说我知道JavaScript的内容吗?
Is there a set of things that every JavaScript programmer should know to be able to say "I know JavaScript"?
推荐答案
不是jQuery。不是YUI。不是(等等)
框架可能很有用,但它们经常隐藏有关JavaScript和DOM实际工作方式的丑陋细节您。如果您的目标是能够说我知道JavaScript,那么在框架中投入大量时间就与此相反。
Frameworks may be useful, but they are often hiding the sometimes-ugly details of how JavaScript and the DOM actually work from you. If your aim is to be able to say "I know JavaScript", then investing a lot of time in a framework is opposed to that.
以下是一些JavaScript语言功能你应该知道要弄清楚它在做什么并且没有被抓住,但这对许多人来说并不是很明显:
Here are some JavaScript language features that you should know to grok what it's doing and not get caught out, but which aren't immediately obvious to many people:
-
那个
object.prop
和对象['prop']
是一回事(所以请你停止使用eval
,谢谢);对象属性总是字符串(即使对于数组); (及。
That
object.prop
andobject['prop']
are the same thing (so can you please stop usingeval
, thanks); that object properties are always strings (even for arrays); whatfor
...in
is for (and what it isn't).
物业嗅探; undefined
是什么(和);为什么在运算符中看似鲜为人知的是有益的,与
typeof
/ undefined 检查;
hasOwnProperty
; 删除
的目的。
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
.
Number
数据类型实际上是一个浮点数;使用花车的语言独立困难;避免 parseInt
八进制陷阱。
That the
Number
datatype is really a float; the language-independent difficulties of using floats; avoiding the parseInt
octal trap.
嵌套函数作用域;在你希望避免意外全局变量的范围内使用
var
的必要性;范围如何用于闭包; 。
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.
全局变量和
窗口
属性如何冲突;全局变量和文档元素不应该如何冲突,而是在IE中发生冲突;在全局范围内使用 var
的必要性也是为了避免这种情况。
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.
如何
function
语句对''前面的代码之前的定义;函数语句和函数表达式之间的区别;为什么命名函数表达式。
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.
构造函数如何运作,
prototype
属性和 new
运算符确实有效; 你真正想要的子类/实例系统;当你可能想要使用基于闭包的对象而不是原型时。 (大多数JS教程材料在这方面都非常糟糕;我需要花费数年的时间才能完全理解它。)
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.)
如何
这个
是在通话时确定的,不受约束;因此,如何方法传递来自其他语言;如何使用闭包或函数#bind
来解决这个问题。
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.
其他ECMAScript第五版功能喜欢
indexOf
, forEach
和函数式编程;如何修复旧浏览器以确保您可以使用它们;使用它们与内联匿名函数表达式来获得紧凑,可读的代码。
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.
浏览器和用户代码之间的控制流程;同步和异步执行;在控制流(例如焦点)内发生的事件与控制返回时发生的事件和超时;如何调用像
alert
这样的同步内置结构可能会导致潜在的灾难性重入。
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.
跨窗口脚本如何影响
instanceof
;跨窗口脚本如何影响跨不同文档的控制流;如何 postMessage
希望能解决这个问题。
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%同意他的好零件)。
Most of all, you should be viewing JavaScript critically, acknowledging that it is for historical reasons an imperfect language (even more than most languages), and avoiding its worst troublespots. Crockford's work on this front is definitely worth reading (although I don't 100% agree with him on which the "Good Parts" are).
这篇关于每个JavaScript程序员应该知道什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!