Skip to content

Latest commit

 

History

History
197 lines (153 loc) · 7.61 KB

File metadata and controls

197 lines (153 loc) · 7.61 KB

Resources

http://regexr.com/

https://jex.im/regulex/#!embed=false&flags=&re=%5E(a%7Cb)*%3F%24

V8 performance rival that of JVM and CLR. Let that sink in a bit.

If you're creating constructor functions and inheriting from them, you haven't learned JavaScript. You're failing to take advantage of JavaScript's most powerful capabilities: Prototypal Inheritance.

JavaScript dynamic duo (static type is overrated):

Debugging

console.log( '%c %s %s %s ', 'color: yellow; background-color: black;', ' — ', 'hello', ' — ');

console.trace('debug')

Design Patterns

Closures

Objects are merely a poor man's closures.

Closures are a poor man's object.

Closures are the key to encapsulation in JavaScript. They enable true data privacy for objects, and protect against function side effects.

Having a poor understanding of how closures work can cause tight coupling and side-effects when they're misused, which quickly leads to unmaintainable code and a breeding ground for bugs.

Closures minimize shared state and side-effects.

V8

JavaScript engine. JavaScript queue. Actual OS events.

People

Macros

macro foo {
  rule { $x plus $y } => {
    $x + $y
  }
}

foo 5 plus 6 // -> 5 + 6
// ES6 Fat arrow
macro => {
  rule infix { ($value (,) ...) | ($body ...) } => {
    function($value (,) ...) {
      $body ...
    }.bind(this)
  }
  rule infix { ($value (,) ...) | $guard:expr } => {
    function($value (,) ...) {
      return $guard;
    }.bind(this)
  }
  rule infix { $param:ident | $guard:expr } => {
    function($param) {
      return $guard;
    }.bind(this)
  }
}

// To use it
(x, y, z) => {
  return x * y * z;
}

Isomorphic

Use cases:

  • Templating
  • Routing
  • I18n
  • Date and currency formatting
  • Model validation
  • API interaction

Magic happens in the build process: Browserify and Grunt.

Equality

4 equality algorithms in ES6:

  • Abstract Equality Comparison ==
  • Strict Equality Comparison ===. Used in case statement, Array.prototype.indexOf, Array.prototype.lastIndexOf
  • SameValueZero. ArrayBuffer as well as Map and Set. Also when doing [NaN].includes(NaN)
  • SameValue - Used in all other places

Unicode

Problem with surrogate pair like "pile of poo" U+1F4A9

function getSurrogates(codePoint) {
  var high = Math.floor((codePoint - 0x10000) / 0x400) + 0xD800;
  var low = (codePoint - 0x10000) % 0x400 + 0xDC00;
  return [high, low];
}

function getCodePoint(high, low) {
  var codePoint = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000;
  return codePoint;
}

// ES6
Array.from(string).length;

function countSymbolsPedantically(string) {
  // Unicode Normalization, NRC form:
  var normalized = string.normalize('NFC');
  // Account for astral symbols / surrogates:
  return Array.from(normalized).length;
}