From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/learn/javascript/howto/index.html | 294 ++++++++++++++++++++++++++ 1 file changed, 294 insertions(+) create mode 100644 files/zh-cn/learn/javascript/howto/index.html (limited to 'files/zh-cn/learn/javascript/howto') diff --git a/files/zh-cn/learn/javascript/howto/index.html b/files/zh-cn/learn/javascript/howto/index.html new file mode 100644 index 0000000000..0aaa32f374 --- /dev/null +++ b/files/zh-cn/learn/javascript/howto/index.html @@ -0,0 +1,294 @@ +--- +title: 在JavaSctript中解决问题 +slug: learn/JavaScript/Howto +tags: + - JavaScript + - 初学者 + - 学习 +translation_of: Learn/JavaScript/Howto +--- +
{{LearnSidebar}}
+ +

以下链接指向您需要修复的日常常见问题的解决方案,以使您的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
+}
+ +

基本定义

+ +
+ + + +
+ +

基本用例

+ +
+ + +
+

数组

+ + + +

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

+ + + +

循环/迭代

+ + +
+
+ +

中级用例

+ +
+ + + +
-- cgit v1.2.3-54-g00ecf