解决 JavaScript 代码中的常见问题

以下链接指向你在编写 JavaScript 时可能遇到的常见问题的解决方案。

¥The following links point to solutions to common problems you may encounter when writing JavaScript.

初学者常见的错误

¥Common beginner's mistakes

正确的拼写和大小写

¥Correct spelling and casing

如果你的代码不起作用和/或浏览器抱怨某些内容未定义,请检查你是否正确拼写了所有变量名称、函数名称等。

¥If your code doesn't work and/or the browser complains that something is undefined, check that you've spelt all your variable names, function names, etc. correctly.

导致问题的一些常见内置浏览器功能包括:

¥Some common built-in browser functions that cause problems are:

正确的 错误的
getElementsByTagName() getElementByTagName()
getElementsByName() getElementByName()
getElementsByClassName() getElementByClassName()
getElementById() getElementsById()

分号位置

¥Semicolon position

你需要确保没有错误地放置任何分号。例如:

¥You need to make sure you don't place any semicolons incorrectly. For example:

正确的 错误的
elem.style.color = 'red'; elem.style.color = 'red;'

函数

¥Functions

函数可能会出现很多问题。

¥There are a number of things that can go wrong with functions.

最常见的错误之一是声明函数,但不在任何地方调用它。例如:

¥One of the most common errors is to declare the function, but not call it anywhere. For example:

js
function myFunction() {
  alert("This is my function.");
}

除非你使用以下语句调用此代码,否则它不会执行任何操作:

¥This code won't do anything unless you call it with the following statement:

js
myFunction();

函数作用域

¥Function scope

请记住,函数有自己的作用域 - 你无法从函数外部访问函数内部设置的变量值,除非你全局声明该变量(即不在任何函数内部),或者从函数中访问 返回值

¥Remember that functions have their own scope — you can't access a variable value set inside a function from outside the function, unless you declared the variable globally (i.e. not inside any functions), or return the value from the function.

在 return 语句后运行代码

¥Running code after a return statement

还要记住,当你从函数返回时,JavaScript 解释器会退出该函数 - return 语句之后的任何代码都不会运行。

¥Remember also that when you return from a function, the JavaScript interpreter exits the function — no code after the return statement will run.

事实上,如果 return 语句后有代码,某些浏览器(如 Firefox)会在开发者控制台中向你显示错误消息。Firefox 为你提供 "return 语句后无法访问代码"。

¥In fact, some browsers (like Firefox) will give you an error message in the developer console if you have code after a return statement. Firefox gives you "unreachable code after return statement".

对象表示法与普通赋值

¥Object notation versus normal assignment

当你在 JavaScript 中正常分配某些内容时,你使用单个等号,例如:

¥When you assign something normally in JavaScript, you use a single equals sign, e.g.:

js
const myNumber = 0;

然而,对于 对象,你需要注意使用正确的语法。该对象必须用大括号括起来,成员名称与其值必须使用冒号分隔,并且成员必须用逗号分隔。例如:

¥With Objects, however, you need to take care to use the correct syntax. The object must be surrounded by curly braces, member names must be separated from their values using colons, and members must be separated by commas. For example:

js
const myObject = {
  name: "Chris",
  age: 38,
};

基本定义

基本用例

¥Basic use cases

一般的

变量

数学

字符串

数组

调试 JavaScript

¥Debugging JavaScript

有关 JavaScript 调试的更多信息,请参阅 处理常见的 JavaScript 问题。另请参阅 其他常见错误 了解常见错误的说明。

¥For more information on JavaScript debugging, see Handling common JavaScript problems. Also, see Other common errors for a description of common errors.

在代码中做出决策

循环/迭代

中间用例

¥Intermediate use cases

函数

对象

JSON

事件

面向对象的 JavaScript

网络 API