JavaScript回锅笔记

  1. Variables created without the keyword var, are always global, even if they are created inside a function.
  2. A closure is a function having access to the parent scope, even after the parent function has closed.
  3. A JavaScript function can be invoked without being called.
  4. 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); }

  5. Avoid String, Number, and Boolean objects. They complicate your code and slow down execution speed.
  6. 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.
  7. In HTML, the global scope is the window object: All global variables belong to the window object.
  8. Your global variables (or functions) can overwrite window variables (or functions).
    Any function, including the window object, can overwrite your global variables and functions.
  9. W3Schools JavaScript Reference HTML DOM Events
  10. 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.)

  11. 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:
  12. In JavaScript, all data types have a valueOf() and a toString() method.
  13. JavaScript counts months from 0 to 11. January is 0. December is 11.
  14. Bit operators work on 32-bit numbers.
  15. The constructor property returns the constructor function for all JavaScript variables.
  16. The unary + operator can be used to convert a variable to a number:
  17. 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
  18. Complete JavaScript RegExp Reference
  19. 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).
  20. Do not use tabs (tabulators) for indentation. Different editors interpret tabs differently.
  21. Avoid global variables, avoid new, avoid ==, avoid eval()