- Variables created without the keyword var, are always global, even if they are created inside a function.
- A closure is a function having access to the parent scope, even after the parent function has closed.
- A JavaScript function can be invoked without being called.
-
Accessing a function without () will return the function definition:.
<script> function toCelsius(f) { return (5/9) * (f-32); } document.getElementById("demo").innerHTML = toCelsius; </script>
输出:
function toCelsius(f) { return (5/9) * (f-32); }
- Avoid String, Number, and Boolean objects. They complicate your code and slow down execution speed.
- If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable, even if it is executed inside a function. Do NOT create global variables unless you intend to.
- In HTML, the global scope is the window object: All global variables belong to the window object.
-
Your global variables (or functions) can overwrite window variables (or functions).
Any function, including the window object, can overwrite your global variables and functions. - W3Schools JavaScript Reference HTML DOM Events
-
You can also break up a code line within a text string with a single backslash:
document.getElementById("demo").innerHTML = "Hello \ Dolly!";
(The \ method is not a ECMAScript (JavaScript) standard.Some browsers do not allow spaces behind the \ character.)
- Integers (numbers without a period or exponent notation) are considered accurate up to 15 digits. The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:
- In JavaScript, all data types have a valueOf() and a toString() method.
- JavaScript counts months from 0 to 11. January is 0. December is 11.
- Bit operators work on 32-bit numbers.
- The constructor property returns the constructor function for all JavaScript variables.
- The unary + operator can be used to convert a variable to a number:
-
5 + null // returns 5 because null is converted to 0
"5" + null // returns "5null" because null is converted to "null"
"5" + 2 // returns 52 because 2 is converted to "2"
"5" - 2 // returns 3 because "5" is converted to 5
"5" * "2" // returns 10 because "5" and "2" are converted to 5 and 2 - Complete JavaScript RegExp Reference
- Hoisting is JavaScript’s default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).
- Do not use tabs (tabulators) for indentation. Different editors interpret tabs differently.
- Avoid global variables, avoid new, avoid ==, avoid eval()