在JavaSctript中解决问题
以下链接指向您需要修复的日常常见问题的解决方案,以使您的JavaScript代码正确运行。
初学者常见的错误
正确的拼写和使用
如果你的代码不工作或浏览器抱怨某些东西是未定义的,请检查你是否正确拼写了所有的变量名称,函数名称等。
导致问题的一些常见的内置浏览器函数有:
正确 | 错误 |
---|---|
getElementsByTagName() |
getElementbyTagName() |
getElementsByName() |
getElementByName() |
getElementsByClassName() |
getElementByClassName() |
getElementById() |
getElementsById() |
分号位置
必须确保没有错误的放置分号,例如:
正确 | 错误 |
---|---|
elem.style.color = 'red'; |
elem.style.color = 'red;' |
函数
函数有很多容易出错的地方。
最常见的错误之一是函数被声明了却没有被调用。例如:
function myFunction() {
alert('This is my function.');
};
这个函数不会执行,除非你调用它,例如:
myFunction();
函数作用域
记住函数拥有自己的作用域——你不能从函数外部访问一个函数内的变量值,除非你在全局声明了该变量(即不在任何函数内),或者从函数外部获得它的返回值。
在return语句之后运行代码
还要记住,当你向一个函数外部返回一个值时,JavaScript解释器会退出这个函数——在return语句运行之后,没有声明任何代码。
事实上,如果您在返回语句之后有代码,某些浏览器(如Firefox)会在开发人员控制台中给您一条错误消息。 Firefox在返回语句后给你提示“无法访问的代码”。
对象标记法与正常赋值
当你在JavaScript中正常赋值时,使用等号:
var myNumber = 0;
但是在对象中,你需要使用冒号来分隔成员名称和值,并用逗号分隔每个成员,例如:
var myObject = {
name : 'Chris',
age : 38
}
基本定义
基本用例
常见
变量
数字
- What types of number do you have to deal with in web development?
- How do you do basic math in JavaScript?
- What is operator precedence, and how is it handled in JavaScript?
- How do you increment and decrement values in JavaScript?
- How do you compare values in JavaScript? (e.g. to see which one is bigger, or to see if one value is equal to another).
字符串
- How do you create a string in JavaScript?
- Do you have to use single quotes or double quotes?
- How do you escape characters in strings?
- How do you join strings together?
- Can you join strings and numbers together?
- How do you find the length of a string?
- How you find what character is at a certain position in a string?
- How do you find and extract a specific substring from a string?
- How do you change the case of a string?
- How do you replace one specific substring with another?
数组
JavaScript 调试
For more information on JavaScript debugging, see Handling common JavaScript problems; also see Other common errors for a description of common errors.
Making decisions in code
- How do you execute different blocks of code, depending on a variable's value or other condition?
- How do you use if ...else statements?
- How do nest one decision block inside another?
- How do you use AND, OR, and NOT operators in JavaScript?
- How do you conveniently handle a large number of choices for one condition?
- How do you use a ternary operator to make a quick choice between two options based on a true or false test?
循环/迭代
- How do you run the same bit of code over and over again?
- How do you exit a loop before the end if a certain condition is met?
- How do you skip to the next iteration of a loop if a certain condition is met?
- How do you use while and do ... while loops?
- How to iterate over the elements in an array
- How to iterate over the elements in a multidimensional array
- How to iterate over the members in an object
- How to iterate over the members of an object nested inside an array
中级用例
函数
- How do you find functions in the browser?
- What is the difference between a function and a method?
- How do you create your own functions?
- How do you run (call, or invoke) a function?
- What is an anonymous function?
- How do you specify parameters (or arguments) when invoking a function?
- What is function scope?
- What are return values, and how do you use them?
对象
- How do you create an object?
- What is dot notation?
- What is bracket notation?
- How do you get and set the methods and properties of an object?
- What is
this
, in the context of an object? - What is object-oriented programming?
- What are constructors and instances, and how do you create them?
- What different ways are there to create objects in JavaScript?
JSON
事件
- What are event handlers and how do you use them?
- What are inline event handlers?
- What does the
addEventListener()
function do, and how do you use it? - Which mechanism should I use to add event code to my web pages?
- What are event objects, and how do you use them?
- How do you prevent default event behaviour?
- How do events fire on nested elements? (event propagation, also related — event bubbling and capturing)
- What is event delegation, and how does it work?
面向对象的JavaScript
- What are object prototypes?
- What is the constructor property, and how can you use it?
- How do you add methods to the constructor?
- How do you create a new constructor that inherits its members from a parent constructor?
- When should you use inheritance in JavaScript?
Web APIs