@manhng

Welcome to my blog!

JavaScript eBooks

August 27, 2018 17:23

JavaScript eBooks (edit)

https://github.com/divyanshu-rawat/JS-Worth-Read

http://pepa.holla.cz/wp-content/uploads/2015/11/Speaking-JavaScript.pdf

The "use strict"

Learn: https://www.bitdegree.org/learn/javascript-use-strict/

Code: https://docs.microsoft.com/en-us/outlook/add-ins/addin-tutorial

Promises for asynchronous programming

const isSmallThenTen = (num) => {
    return new Promise((resolve, reject) => {
        if(num < 10) {
            resolve(true)
        } else {
            reject(false)
        }
    })
};

isSmallThenTen(11)
    .then(res => console.log('The number is smaller then 10'))
    .catch(err => console.log('The number is not smaller then 10'));

Arrow function inline

var array = [1, 2, 3, 4]

var sumOfArrayElements = array.reduce((acc, value) => acc + value, 0)
var productOfArrayElements = array.reduce((acc, value) => acc * value, 1)

Arrow function complex

var array = [1, 2, 3, 4]

const sum = (acc, value) => {
  const result = acc + value
  console.log(acc, ' plus ', value, ' is ', result)
  return result
}
var sumOfArrayElements = array.reduce(sum, 0)

Template strings

function hello(firstName, lastName) {
  return `Good morning ${firstName} ${lastName}! 
How are you?`
}

console.log(hello('Jan', 'Kowalski'))

The Closures

Closures explained with JavaScript

The Module Pattern

var counter = (function(){
    var i = 0;

    return {
      get: function(){
        return i;
      },
      set: function( val ){
        i = val;
      },
      increment: function() {
        return ++i;
      }
    };
}());

The Immediately-Invoked Function Expression (IIFE) Pattern

(function () { // open IIFE
    var tmp = ...; // not a global variable
}()); // close IIFE

http://benalman.com/news/2010/11/immediately-invoked-function-expression/

IIFE use case ???

Categories

Recent posts