67 Matching Annotations
- Jul 2018
-
speakingjs.com speakingjs.com
-
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.
-
(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 = ...; ... }
-
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); }
-
var
-
var
-
var
-
var
-
var
-
var
-
var
-
var
-
var
-
var
-
var
-
var
-
function
-
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.
-
Math.pow
-
Math.pow
-
function
-
function
-
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.
-
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
-
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); }); }
-
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
-
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'
-
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
-
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.
-
var
-
In JavaScript, you declare variables via var before using them:> var x; > x undefined > y ReferenceError: y is not defined
-
var
-
You can declare and initialize several variables with a single var statement:var x = 1, y = 2, z = 3;
-
function
-
function
-
var
-
To concatenate strings in multiple steps, use the += operator:> var str = ''; > str += 'Multiple '; > str += 'pieces '; > str += 'are concatenated.';
-
'You have ' + messageCount + ' messages'
-
"That's nice!"
-
"Did she say \"Hello\"?"
-
== and !=
-
==, !=
-
Increment: ++variable, variable++ Decrement: --variable, variable--
-
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.
-
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
-
var
-
var
-
var
-
var
-
var
-
var
-
var
-
var
-
var
-
"abc"
-
var
-
var
-
var
-
var
-
var
-
function
-
var
-
var
-
var
-
var
-
var
-
var
-
// single-line comment
Tags
Annotators
URL
-