Skip to content

Instantly share code, notes, and snippets.

@nodew
Created October 22, 2016 16:43
Show Gist options
  • Save nodew/058db89e1a79365663bc99aec43d2e55 to your computer and use it in GitHub Desktop.
Save nodew/058db89e1a79365663bc99aec43d2e55 to your computer and use it in GitHub Desktop.
function store() {}
function closure(obj) {
store.print = function() {
console.log(obj)
}
return function() {
store.print()
}
}
var c1 = closure({a: 1, b: 2})
c1()
var c2 = closure({a: 3, b: 4})
c2()
c1()
/**
* the outout is
* { a: 1, b: 2 }
* { a: 3, b: 4 }
* { a: 3, b: 4 }
*/
@ltchronus
Copy link

ltchronus commented Oct 24, 2016

function Store() {}

function closure(obj) {
  var store = new Store();
  store.print = function() {
    console.log(obj)
  }
  return function() {
    store.print()
  }
}

var c1 = closure({a: 1, b: 2})
c1()
var c2 = closure({a: 3, b: 4})
c2()
c1()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment