Skip to content

Latest commit

 

History

History
57 lines (41 loc) · 880 Bytes

function.md

File metadata and controls

57 lines (41 loc) · 880 Bytes

Function

argument-default-value

var getDefault = function () {
  return 4;
};

function a(a, b = ++a, c = getDefault()) {
  console.log(c); // 4
  console.log(a); // 1
  console.log(b); // 2
}

a(1, 2);

validate-mandatory-field

function throwError() {
  throw new Error('Missing parameter');
}

function a(param1 = throwError()) {

}

a(10); // ok
a(); // Error: missing parameter

iife-es6

(global => {
  const MY_CONSTANT = 'api key or something'
  let counter = 0
  let some_array = [1,2,34,5,6,7]

  counter = some_array.reduce (total, item) => total + item
})(window)

or if you don't need window object:

()=> {
  // code goes here...
}()