每个 JavaScript 程序员都应该知道什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2628672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:13:24  来源:igfitidea点击:

What should every JavaScript programmer know?

javascriptprogramming-languages

提问by gath

Is there a set of things that every JavaScript programmer should know to be able to say "I know JavaScript"?

有没有一套东西是每个 JavaScript 程序员都应该知道的才能说“我知道 JavaScript”?

回答by bobince

Not jQuery. Not YUI. Not (etc. etc.)

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

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 和 DOM 实际工作方式的有时丑陋的细节。如果您的目标是能够说“我知道 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:

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

  • That object.propand 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...inis for(and what it isn't).

  • Property-sniffing; what undefinedis (and why it smells); why the seemingly-little-known inoperator is beneficial and different from typeof/undefinedchecks; hasOwnProperty; the purpose of delete.

  • That the Numberdatatype is really a float; the language-independent difficulties of using floats; avoiding the parseIntoctal trap.

  • Nested function scoping; the necessity of using varin the scope you want to avoid accidental globals; how scopes can be used for closures; the closure loop problem.

  • How global variables and windowproperties collide; how global variables and document elements shouldn't collide but do in IE; the necessity of using varin global scope too to avoid this.

  • How the functionstatement 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 prototypeproperty and the newoperator really work; methodsof 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 thisis determined at call-time, not bound; how consequently method-passing doesn't work like you expectfrom other languages; how closures or Function#bindmay be used to get around that.

  • Other ECMAScript Fifth Edition features like indexOf, forEachand 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 alertcan 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 postMessagewill hopefully fix this.

  • object.propobject['prop']是一样的东西(所以你能不能停止使用eval,谢谢);对象属性总是字符串(即使是数组);什么for......in是为了(以及它不是什么)。

  • 属性嗅探;是什么undefined(以及为什么有气味);为什么看似鲜为人知的in运算符是有益的,并且与typeof/undefined检查不同;hasOwnProperty; 的目的delete

  • Number数据类型是一个真正的浮动; 使用浮动的与语言无关的困难;避免parseInt八进制陷阱。

  • 嵌套函数作用域;在var您希望避免意外全局变量的范围内使用的必要性;作用域如何用于闭包;在闭合回路问题

  • 全局变量和window属性如何冲突;全局变量和文档元素如何在 IE 中不发生碰撞但发生碰撞;也有必要var在全局范围内使用以避免这种情况。

  • function语句如何在其前面的代码之前“提升”一个定义;函数语句和函数表达式的区别;为什么不应使用命名函数表达式。

  • 构造函数、prototype属性和new运算符如何真正起作用;方法利用这个来创建普通的类/子类/实例,你其实是想系统; 当您可能想要使用基于闭包的对象而不是原型设计时。(大多数 JS 教程材料在这方面都非常糟糕;我花了好几年才把它弄清楚。)

  • 如何this在调用时确定,不受约束;因此,方法传递如何不像您对其他语言所期望的那样工作;如何关闭或Function#bind可以用来解决这个问题。

  • 其他 ECMAScript 第五版特性,如indexOfforEach以及 上的函数式编程方法Array;如何修复旧浏览器以确保您可以使用它们;将它们与内联匿名函数表达式一起使用以获得紧凑、可读的代码。

  • 浏览器和用户代码之间的控制流程;同步和异步执行;在控制流(例如焦点)内触发的事件与控制返回时发生的事件和超时;如何调用一个所谓的同步内置alert函数最终会导致潜在的灾难性重入。

  • 跨窗口脚本如何影响instanceof;跨窗口脚本如何影响跨不同文档的控制流;postMessage希望如何解决这个问题。

See this answerregarding the last two items.

请参阅有关最后两项的答案

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,承认由于历史原因,它是一种不完美的语言(甚至比大多数语言都多),并避免其最严重的问题。Crockford 在这方面的工作绝对值得一读(尽管我并不 100% 同意他的“好部分”)。

回答by graphicdivine

That it can be disabled.

它可以被禁用。

回答by bron

Understanding the stuff written in Crockford's Javascript: The Good Partsis a pretty good assumption that a person is a decent JS programmer.

理解用 Crockford 的Javascript: The Good Parts编写的东西是一个很好的假设,即一个人是一个体面的 JS 程序员。

You can pretty much know how to use a good library like JQuery and still not know the hidden parts of Javascript.

您几乎可以知道如何使用像 JQuery 这样的好库,但仍然不知道 Javascript 的隐藏部分。

Another note is Debugging tools on various browsers. A JS programmer should know how to debug his code in different browsers.

另一个注意事项是各种浏览器上的调试工具。JS 程序员应该知道如何在不同浏览器中调试他的代码。

Oh! And knowing JSLint will totally hurt your feelings!!

哦!并且知道 JSLint 会完全伤害你的感情!!

回答by Skilldrick

If you want to be a true JavaScript ninja, you should know the answers to every question in the Perfection kills JavaScript Quiz.

如果您想成为一名真正的 JavaScript 忍者,您应该知道Perfection kills JavaScript Quiz 中每个问题的答案。

An example to whet your appetite:

一个让你食欲大开的例子:

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

What does this expression return?

  • “number”
  • “undefined”
  • “function”
  • Error

这个表达式返回什么?

  • “数字”
  • “不明确的”
  • “功能”
  • 错误

回答by edwin

You don't know JavaScript if you don't know:

如果你不知道,你就不会知道 JavaScript:

  1. Closures
  2. Prototype-based inheritance
  3. The module pattern
  4. The W3C-DOM
  5. How events work
  1. 关闭
  2. 基于原型的继承
  3. 模块模式
  4. W3C-DOM
  5. 事件如何运作

回答by Sripathi Krishnan

..that javascript is not java :)

..那个 javascript 不是 java :)

Many, many people starting with website development have told me javascript is just simple java!

很多很多从网站开发开始的人都告诉我 javascript 只是简单的 java!

回答by David

  1. Familiarize yourself with atleast one Javascript library ( Jquery, Prototype, etc ).

  2. Learn how to use the debugging tools of the major browsers ( MSIE 7-8, Firefox, Chrome, Safari )

  3. Read up on the industry: Douglas Crockford's website is a treasure trove while Ajaxian.com is a good blog to keep up on new, interesting, and or odd ideas for Javascript. There are a number of other resources but those are the ones that helped me the most.

  1. 熟悉至少一个 Javascript 库(Jquery、Prototype 等)。

  2. 了解如何使用主流浏览器(MSIE 7-8、Firefox、Chrome、Safari)的调试工具

  3. 深入了解该行业:Douglas Crockford 的网站是一个宝库,而 Ajaxian.com 是一个很好的博客,可以了解有关 Javascript 的新的、有趣的或奇怪的想法。还有许多其他资源,但这些资源对我帮助最大。

回答by Sarfraz

Javascript objects and functionas first-class citizen, callbacks, not to forget about eventsand then JQuery.

Javascript对象和函数作为一等公民回调,不要忘记事件,然后是JQuery

回答by Ashwin Prabhu

That Javascript is not something which can be learnt in an hour!

Javascript 不是一个小时就能学会的!

回答by theycallmemorty

Variables are global unless declared to be local!!

变量是全局的,除非声明为本地变量!!

Bad (DoSomething() is only called 10 times):

不好(DoSomething() 只调用了 10 次):

function CountToTen()
{
  for(i=0; i< 10; i++)
  {
    DoSomething(i);
  }
}

function countToFive()
{
  for(i=0; i<5; i++)
  {
    CountToTen();
  }
}

CountToFive();

Good (DoSomething() is called 50 times as intended):

好(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();