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()

JavaScript里的奇葩语法

排名不分先后。

  1. "1234"+5的结果是"12345", 而"1234"-4的结果却是1230. 4+"1234"的结果是"41234", 而4-"1234"的结果是NaN.
  2. true==2的结果是false, false==2的结果也是false.
  3. typeof null的结果是"object".
  4. 对于str.slice(start, end),如果参数大于等于0,则结果不包括end指示的字符;如果参数小于0,则结果不包括start指示的字符.
  5. var x = 0.2 + 0.1; // x will be 0.30000000000000004
  6. typeof NaN; // returns "number"
    var x = NaN + "5"; var y = Infinity + "3"; // x will be "NaN5", and y will be "Infinity3"
  7. 0 < Number.MIN_VALUE // true
  8. var txt = "";
    var person = ["23", "12f", 2];
    var x;
    for (x in person) {
    txt += typeof x + " ";
    }

    最成txt为string string string