--- title: JavaScript 解決常見的問題 slug: Learn/JavaScript/Howto translation_of: Learn/JavaScript/Howto ---
如果您的代碼毫無反映或瀏覽器反饋某些內容「未定義」,請檢查您是否「正確輸入」所有變量名稱,函數名稱等。
以下的常見造成問題的內置瀏覽器功能比對:
正確 | 錯誤 |
---|---|
getElementsByTagName() |
getElementbyTagName() |
getElementsByName() |
getElementByName() |
getElementsByClassName() |
getElementByClassName() |
getElementById() |
getElementsById() |
You need to make sure you don't place any semi-colons incorrectly. For example:
Correct | Wrong |
---|---|
elem.style.color = 'red'; |
elem.style.color = 'red;' |
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:
function myFunction() { alert('This is my function.'); };
This code won't do anything unless you call it, for example with
myFunction();
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 out of the function.
Remember also that when you return a value out of a function, the JavaScript interpreter exits the function — no code declared after the return statement will run.
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".
When you assign something normally in JavaScript, you use a single equals sign, e.g.:
const myNumber = 0;
This doesn't work in Objects, however — with objects you need to separate member names from their values using colons, and separate each member with a comma, for example:
const myObject = { name: 'Chris', age: 38 }
For more information on JavaScript debugging, see Handling common JavaScript problems; also see Other common errors for a description of common errors.
this
, in the context of an object?addEventListener()
function do, and how do you use it?