Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript: Basic rules for avoiding strange is...

denkeni
March 12, 2015

JavaScript: Basic rules for avoiding strange issues

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

denkeni

March 12, 2015
Tweet

More Decks by denkeni

Other Decks in Programming

Transcript

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

    console.log(num); Bad example 1
  2. window.num = 1; (function () { window.num = 2; })();

    console.log(window.num); // 2 What actually happened
  3. num = 1; (function () { var num = 2;

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

    var num = 2; })(); console.log(num); // 1 })(); Even Better example 1 don't code in global scope
  5. 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. Bad example 2 (function () { var num = 1;

    (function () { console.log(num); var num = 2; // no warning here! console.log(num); })(); console.log(num); })();
  7. 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. 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. Better example 2 (function () { var num = 1;

    (function () { console.log(num); // 1 var num2 = 2; console.log(num2); // 2 })(); console.log(num); // 1 })();
  10. 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. Even Better example 3 (function () { var num =

    1; (function () { var num = 2; console.log(num); // 2 })(); console.log(num); // 1 })();
  12. • There's no block scope! • Use functional scope to

    avoid global Only Functional Scope
  13. (function () { var num = 1; console.log(num); // 1

    if (true) { var num = 2; console.log(num); // 2 } console.log(num); // 2 })(); Bad example
  14. (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. 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. 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. 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. 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. 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. 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. var num = 0.1 * 2; if (num === 0.2)

    { console.log("Correct."); // Correct. } else { console.log(num); }
  22. var num = 0.1 * 0.2; if (num === 0.02)

    { console.log("Correct."); } else { console.log(num); // 0.020000000000000004 } Bad example
  23. var num = 0.1 * 0.2; if (num === 0.02)

    { console.log("Correct."); } else { console.log(num); // 0.020000000000000004 } Bad example
  24. var resultString = (0.1 * 0.2).toFixed(2); if (resultString === "0.02")

    { console.log("Correct."); // Correct. } else { console.log(num); } Better example
  25. Sum up • Use "var" and declare all variables at

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