closure - douglas crockford

by Alejandro M

CSS

body{background:#fff}

JavaScript

var myObject = function() {
  var value = 10;
  return {
    increment: function(inc) {
      value += typeof inc === 'number' ? inc : 1;
    },
    getValue: function() {
      return value;
    }
  }
}();
myObject.increment(2);
console.log(myObject.getValue())


// Create a maker function called quo. It makes an
// object with a get_status method and a private
// status property.

var quo = function(status) {
  return {
    get_status: function() {
      return status;
    }
  };
};
// Make an instance of quo.
var myQuo = quo("amazed");
console.log(myQuo.get_status());


//// Define a function that sets a DOM node's color
// to yellow and then fades it to white.
var fade = function(node) {
  var level = 15;
  var step = function() {
    var hex = level.toString(16);
    console.log("hex", hex)
    node.style.backgroundColor = '#FFFF' + hex + hex;
    if (level > 1) {
      level -= 1;
      setTimeout(step, 100);
    }
  };
  setTimeout(step, 100);
};
fade(document.body);