Please enable JavaScript.
Coggle requires JavaScript to display documents.
JavaScript (Weird Parts (Equal is not always equal
[1,2,3] == [1,2,3];…
JavaScript
Weird Parts
Equal is not always equal
[1,2,3] == [1,2,3]; //false
//b/c it is reference type
null is object
incorrect:
if (typeof obj === 'object') { alert(obj.prop); }
correct:
if (typeof obj === 'object' && obj !== null) { alert(obj.prop);}
-
-
-
-
-
-
-
-
-
-
-
-
-
Awful Parts
-
Scope
a variable declared in a block is visible everywhere in the function containing the block. This can be surprising to programmers with experience in other languages.
-
-
typeof
typeof 98.6
produces 'number'. Unfortunately:
typeof null
returns 'object' instead of 'null'. Oops. A better test for null is simply:
my_value === null
A bigger problem is testing a value for objectness. typeof cannot distinguish between null and objects
parseInt
If the first character of the string is 0, then the string is evaluated in base 8 instead of base 10. In base 8, 8 and 9 are not digits, so parseInt("08") and parseInt("09") produce 0 as their result. This error causes problems in programs that parse dates and times.
Floating Point
Binary floating-point numbers are inept at handling decimal fractions, so 0.1 + 0.2 is not equal to 0.3.
NaNtypeof NaN === 'number' // true
The value can be produced by attempting to convert a string to a number when the string is not in the form of a number. For example:
- '0' // 0
- 'oops' // NaN
If NaN is an operand in an arithmetic operation, then NaN will be the result. So, if you have a chain of formulas that produce NaN as a result, at least one of the inputs was NaN, or NaN was generated somewhere.
You can test for NaN. As we have seen, typeof does not distinguish between numbers and NaN, and it turns out that NaN is not equal to itself. So, surprisingly:NaN === NaN // false
NaN !== NaN // true
-
Falsy Values
0 Number
NaN (not a number) Number
'' (empty string) String
false Boolean
null Object
undefined Undefined
ECMAScript 6
-
Block-Scoped Variables
let callbacks = []
for (let i = 0; i <= 2; i++) { callbacks[i] = function () { return i * 2 } }
Arrow Functions
Expression Bodies
odds = evens.map(v => v + 1)
pairs = evens.map(v => ({ even: v, odd: v + 1 }))
nums = evens.map((v, i) => v + i)
-
Lexical this
// variant 1
var self = this;
this.nums.forEach(function (v) { if (v % 5 === 0) self.fives.push(v);});
// variant 2
this.nums.forEach(function (v) { if (v % 5 === 0) this.fives.push(v);}, this);
-
Template Literals
String Interpolation
var customer = { name: "Foo" }
var card = { amount: 7, product: "Bar", unitprice: 42 }
var message = Hello ${customer.name},
want to buy ${card.amount} ${card.product} for
a total of ${card.amount * card.unitprice} bucks?
-
-
-
-
-
Create Object
-
-
-
-
Using the function constructor + prototype:
function myObj(){};
myObj.prototype.name = "hello";
var k = new myObj();
Using ES6 class syntax:
class myObject { constructor(name) { this.name = name; }}
var e = new myObject("hello");
-
-
Good parts
The best part of JavaScript is its extreme flexibilities of functions.
Functions are first class citizens (VIPS)
-
-
-
-
-
-
Hoisting
Function declarations and function variables are always moved (‘hoisted’) to the top of their JavaScript scope by the JavaScript interpreter
The left hand side (var bar) is a Variable Declaration. Variable Declarations get hoisted but their Assignment Expressions don’t. So when bar is hoisted the interpreter initially sets var bar = undefined. The function definition itself is not hoisted.
-