是否有一组东西是每个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,如果你能有效地使用数组,数字,字符串,日期和对象。为Math和RegExp加分。你应该能够编写函数和使用变量(在正确的范围内,即作为对象的“方法”)。
我看到一些关于了解闭包的评论,奢侈的函数语法,blabla。所有这些都与这个问题无关。这就好比说,如果你能在11秒内跑完100米,你就是一名跑步者。
我说可能需要几周的时间来精通javascript。在那之后,你需要花费数年时间,阅读数十本书,编写数千行程序,才能成为专家、忍者等等。
但这不是问题所在。
对了,DOM不是javascript的一部分,jQuery也不是。所以我认为两者都与这个问题无关。
jQuery是我最好的推荐。不仅仅是代码本身,它的习惯用法、风格和背后的思想都是最值得模仿的。
我强烈推荐阅读Javascript: The Good Parts
它可以被禁用。
数组中。Length方法不是数组项的计数,而是最高索引。即使该项被设置为undefined
var a = [];
a.length; // === 0
a[10]; // === undefined
a[10] = undefined;
a.length; // === 11
a.pop(); // === undefined
a.length; // === 10
这种行为很难与语言设计错误区分开来。
推荐文章
- 给一个数字加上st, nd, rd和th(序数)后缀
- 如何以编程方式触发引导模式?
- setTimeout带引号和不带括号的区别
- 在JS的Chrome CPU配置文件中,'self'和'total'之间的差异
- 用javascript检查输入字符串中是否包含数字
- 如何使用JavaScript分割逗号分隔字符串?
- 在Javascript中~~(“双波浪号”)做什么?
- 谷歌chrome扩展::console.log()从后台页面?
- 未捕获的SyntaxError:
- [].slice的解释。调用javascript?
- jQuery日期/时间选择器
- 我如何预填充一个jQuery Datepicker文本框与今天的日期?
- 数组的indexOf函数和findIndex函数的区别
- jQuery添加必要的输入字段
- Access-Control-Allow-Origin不允许Origin < Origin >