JavaScript: Basic rules for avoiding strange issues

JavaScript: Basic rules for avoiding strange issues

JavaScript 地雷部分與實作建議:JavaScript 是一個很容易寫出很難除錯的程式語言;遵循一些準則可以減低這些困擾。

188a42adbd815a746df557c7dc72566a?s=128

denkeni

March 12, 2015
Tweet

Transcript

  1. 4.

    num = 1; (function () { num = 2; })();

    console.log(num); Bad example 1
  2. 5.

    window.num = 1; (function () { window.num = 2; })();

    console.log(window.num); // 2 What actually happened
  3. 6.

    num = 1; (function () { var num = 2;

    })(); console.log(num); // 1 Better example 1 use "var"
  4. 7.

    (function () { var num = 1; (function () {

    var num = 2; })(); console.log(num); // 1 })(); Even Better example 1 don't code in global scope
  5. 8.

    Bad example 2 (function () { var num = 1;

    (function () { console.log(num); // 1; function has closure. num = 2; console.log(num); // 2 })(); console.log(num); // 2 })();
  6. 9.

    Bad example 2 (function () { var num = 1;

    (function () { console.log(num); var num = 2; // no warning here! console.log(num); })(); console.log(num); })();
  7. 10.

    Bad example 2 (function () { var num = 1;

    (function () { console.log(num); // undefined var num = 2; console.log(num); // 2 })(); console.log(num); // 1 })(); var num
  8. 11.

    What actually happened (function () { var num = 1;

    (function () { ; console.log(num); // undefined num = 2; console.log(num); // 2 })(); console.log(num); // 1 })(); var num
  9. 12.

    Better example 2 (function () { var num = 1;

    (function () { console.log(num); // 1 var num2 = 2; console.log(num2); // 2 })(); console.log(num); // 1 })();
  10. 13.

    Even Better example 2 (function () { var num =

    1; (function () { var num2 = 2; console.log(num); // 1 console.log(num2); // 2 })(); console.log(num); // 1 })();
  11. 14.

    Even Better example 3 (function () { var num =

    1; (function () { var num = 2; console.log(num); // 2 })(); console.log(num); // 1 })();
  12. 15.

    • There's no block scope! • Use functional scope to

    avoid global Only Functional Scope
  13. 17.

    (function () { var num = 1; console.log(num); // 1

    if (true) { var num = 2; console.log(num); // 2 } console.log(num); // 2 })(); Bad example
  14. 18.

    (function () { var num = 1; console.log(num); // 1

    if (true) { (function () { var num = 2; console.log(num); // 2 })(); } console.log(num); // 1 })(); Better example
  15. 20.

    var num = 0; if (num) { console.log("1st"); } num

    = num + 1; if (num) { console.log("2nd"); } num = num - 1; if (num) { console.log("3rd") } Result: "2nd"
  16. 21.

    var num = "0"; // mistake here if (num) {

    console.log("1st"); } num = num + 1; if (num) { console.log("2nd"); } num = num - 1; if (num) { console.log("3rd") }
  17. 22.

    var num = "0"; // console.log(num) -> "0" if (num)

    { console.log("1st"); } num = num + 1; // console.log(num) -> "01" if (num) { console.log("2nd"); } num = num - 1; // console.log(num) -> 0 if (num) { console.log("3rd") } Result: "1st" "2nd"
  18. 23.

    var num = "0"; // typeof(num) === "string" if (num)

    { console.log("1st"); } num = num + 1; // typeof(num) === "string" if (num) { console.log("2nd"); } num = num - 1; // typeof(num) === "number" if (num) { console.log("3rd") } Result: "1st" "2nd"
  19. 24.

    var num = "0"; if (num != 0) { console.log("1st");

    } num = num + 1; if (num != 0) { console.log("2nd"); } num = num - 1; if (num != 0) { console.log("3rd") } Result: "2nd"
  20. 25.

    var num = "0"; if (num != 0) { console.log("1st");

    } num = num + 1; if (num != 0) { console.log("2nd"); } num = num - 1; if (num != 0) { console.log("3rd") } Result: "2nd" Bad example
  21. 27.

    var num = 0.1 * 2; if (num === 0.2)

    { console.log("Correct."); // Correct. } else { console.log(num); }
  22. 28.

    var num = 0.1 * 0.2; if (num === 0.02)

    { console.log("Correct."); } else { console.log(num); // 0.020000000000000004 } Bad example
  23. 29.

    var num = 0.1 * 0.2; if (num === 0.02)

    { console.log("Correct."); } else { console.log(num); // 0.020000000000000004 } Bad example
  24. 30.

    var resultString = (0.1 * 0.2).toFixed(2); if (resultString === "0.02")

    { console.log("Correct."); // Correct. } else { console.log(num); } Better example
  25. 31.

    Sum up • Use "var" and declare all variables at

    top • Only functional scope • Use "===" • 0.1*0.2 !==0.3 so use .toFixed()