是否有一组东西是每个JavaScript程序员都应该知道的,以便能够说“我懂JavaScript”?
当前回答
变量是全局的,除非声明为局部的!!
Bad (DoSomething()只被调用10次):
function CountToTen()
{
for(i=0; i< 10; i++)
{
DoSomething(i);
}
}
function countToFive()
{
for(i=0; i<5; i++)
{
CountToTen();
}
}
CountToFive();
好(DoSomething()按预期被调用50次):
function CountToTen()
{
var i;
for(i=0; i< 10; i++)
{
DoSomething(i);
}
}
function countToFive()
{
var i;
for(i=0; i<5; i++)
{
CountToTen();
}
}
CountToFive();
其他回答
在Javascript中,性能很重要。
没有智能编译器来优化你的代码,所以你应该更小心,当你写javascript代码比语言像c#, Java…
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库(Jquery, Prototype等)。 学习如何使用主要浏览器的调试工具(MSIE 7-8, Firefox, Chrome, Safari) 深入了解这个行业:Douglas Crockford的网站是一个宝库,ajajana.com是一个很好的博客,可以让你了解关于Javascript的新的、有趣的或奇怪的想法。还有很多其他的资源,但这些是对我帮助最大的。
如果你不知道以下内容,你就不懂JavaScript:
闭包 基于原型的继承 模块模式 w3c dom的 事件是如何工作的
知道Javascript最初被称为LiveScript,加上“Java”前缀是出于营销目的,而不是因为Java和Javascript有关系(它们并没有关系)。
哦,对于拥有任何版本的David Flanagan的“Javascript:权威指南”(这一信息在第2页)。
... 感谢那些之前试图混淆ie4文档的人。all[]和Netscape Navigator 4的文档。在Jquery之前使用layers[]消除了这种痛苦。
编辑:
正如@Kinopiko指出的,JavaScript最初被称为项目Mocha(一些消息来源也认为它被称为项目LiveWire),但人们普遍认为,该语言(由Brendan Eich编写)在1996年初发布时采用Java前缀之前,计划以LiveScript的形式发布。
推荐文章
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?
- 如何检查动态附加的事件监听器是否存在?