object closure
function Obj() {
let privateCounter = 0
return {
plus: function() {
privateCounter++
},
minus: function() {
privateCounter--
},
state: function() {
return privateCounter
},
}
}
const A = Obj()
const B = Obj()
A.plus()
B.minus()
console.log(A.state(), B.state())
function closure
function Obj() {
let privateCounter = 0
this.plus = function() {
privateCounter++
}
this.minus = function() {
privateCounter--
}
this.state = function() {
return privateCounter
}
}
const A = new Obj()
const B = new Obj()
A.plus()
B.minus()
console.log(A.state(), B.state())