67 Matching Annotations
  1. Jul 2018
    1. Sometimes you want to introduce a new variable scope—for example, to prevent a variable from becoming global. In JavaScript, you can’t use a block to do so; you must use a function.
    2. (function () { // open IIFE var tmp = ...; // not a global variable }()); // close IIFE

      2.3 Note that both let and const are block-scoped. You can replace an IIFE that looks like:

      // bad
      (function () {
         var tmp = ...;
         ...
      })());
      

      with just:

      // good
      {
        const tmp = ...;
        ...
      }
      
    3. for (var i=0; i < 5; i++) { (function () { var i2 = i; // copy current i result.push(function () { return i2 }); }()); }

      2.3 Note that both let and const are block-scoped. You can replace an IIFE that looks like:

      // bad
      for (var i=0; i < 5; i++) {
          (function () {
              var i2 = i; // copy current i
              result.push(function () { return i2 });
          }());
      }
      

      with just:

      // good
      for (let i=0; i < 5; i++) {
          // copy current i
          const i2 = i;
          result.push(() => i2);
      }
      
    4. var
    5. var
    6. var
    7. var
    8. var
    9. var
    10. var
    11. var
    12. var
    13. var
    14. var
    15. var
    16. function
    17. This chapter is about “Basic JavaScript,”

      The highlighted annotations with the airbnb tag were made to illustrate where Basic Javascript differs from the Airbnb Javascript Style Guide and typescript tag for differences from Typescript Deep Dive TIPs.

    18. Math.pow
    19. Math.pow
    20. function
    21. function
    22. Function expressions are often used as arguments in function calls in JavaScript. Always be careful when you refer to this from one of those function expressions.
    23. Or, forEach has a second parameter that allows you to provide a value for this:logHiToFriends: function () { 'use strict'; this.friends.forEach(function (friend) { console.log(this.name+' says hi to '+friend); }, this); }

      Harmful for the same reason as: Bind is Harmful

    24. First, we could store this in a different variable:logHiToFriends: function () { 'use strict'; var that = this; this.friends.forEach(function (friend) { console.log(that.name+' says hi to '+friend); }); }
    25. Every function has its own special variable this. This is inconvenient if you nest a function inside a method, because you can’t access the method’s this from the function
    26. use the method bind() that all functions have. It creates a new function whose this always has the given value:> var func2 = jane.describe.bind(jane); > func2() 'Person named Jane'
    27. This section does not fully explain how JavaScript inheritance works, but it shows you a simple pattern to get you started. Consult Chapter 17 if you want to know more.In addition to being “real” functions and methods, functions play another role in JavaScript: they become constructors—factories for objects—if invoked via the new operator. Constructors are thus a rough analog to classes in other languages. By convention, the names of constructors start with capital letters. For example:// Set up instance data function Point(x, y) { this.x = x; this.y = y; } // Methods Point.prototype.dist = function () { return Math.sqrt(this.x*this.x + this.y*this.y); };We can see that a constructor has two parts. First, the function Point sets up the instance data. Second, the property Point.prototype contains an object with the methods. The former data is specific to each instance, while the latter data is shared among all instances.To use Point, we invoke it via the new operator:> var p = new Point(3, 5); > p.x 3 > p.dist() 5.830951894845301p is an instance of Point:> p instanceof Point true
    28. Variables Are Function-ScopedThe scope of a variable is always the complete function (as opposed to the current block). For example:function foo() { var x = -512; if (x < 0) { // (1) var tmp = -x; ... } console.log(tmp); // 512 }We can see that the variable tmp is not restricted to the block starting in line (1); it exists until the end of the function.
    29. var
    30. In JavaScript, you declare variables via var before using them:> var x; > x undefined > y ReferenceError: y is not defined
    31. var
    32. You can declare and initialize several variables with a single var statement:var x = 1, y = 2, z = 3;
    33. function
    34. function
    35. var
    36. To concatenate strings in multiple steps, use the += operator:> var str = ''; > str += 'Multiple '; > str += 'pieces '; > str += 'are concatenated.';
    37. 'You have ' + messageCount + ' messages'
    38. "That's nice!"
    39. "Did she say \"Hello\"?"
    40. == and !=
    41. ==, !=
    42. Increment: ++variable, variable++ Decrement: --variable, variable--
    43. Binary logical operators: && (And), || (Or)

      Note that these operators do not necessarily produce booleans, as described below, && returns the first Falsy operand and || returns the last Truthy operand, short-circuited.

    44. Boolean(), called as a function, converts its parameter to a boolean. You can use it to test how a value is interpreted:> Boolean(undefined) false > Boolean(0) false > Boolean(3) true > Boolean({}) // empty object true > Boolean([]) // empty array true
    45. var
    46. var
    47. var
    48. var
    49. var
    50. var
    51. var
    52. var
    53. var
    54. "abc"
    55. var
    56. var
    57. var
    58. var
    59. var
    60. function
    61. var
    62. var
    63. var
    64. var
    65. var
    66. var
    67. // single-line comment