Modules and functions

Is function good?

var store = {
    disabled: false
};

//.....

var disable = function(){
    store.disabled = true;
};
var updateState = function(limit){
    if (limit > 10) {
        showBigLabel();
    } else {
        showSmallLabel();
    }
};
var sum = function(a, b){
    var result = a + b;
    
    console.log(result);

    return result;
};

getElementById

var updateSumTitle = fucntion(el, a, b){
    el.querySelector(".result").textContent = a + b;
};
var isEnough = (() => {
    var prevValue = 0;
    
    return (val) => {
        prevValue += val;

        return prevValue > 10;
    };
})();

What is a perfect function

  • Easy to test
  • Can be run in parallel
  • Can be used in memoization pattern
  • Can be used in different environment

Functional programming

Pure functions

  1. independent from context
  2. independent from global
  3. independant from rules

Explicit arguments

var store = {
    computedValue: 42
};

var compute = function(a, b){
    store.computedValue = (store.computedValue + a) / b;
};

Curry and compose

inpure -> pure... -> inpure

Reproducible

Module

Module === function

Module

  • Independent
  • Portable
  • Single responsibility
  • Don't polute external environment

Is module right?

Web Components

БЭМ

Modules and functions

By Vladimir

Modules and functions

  • 122