Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

function - How do JavaScript closures work?

function createObject() {
  let x = 42;
  return {
    log() { console.log(x) },
    increment() { x++ },
    update(value) { x = value }
  }
}

const o = createObject()
o.increment()
o.log() // 43
o.update(5)
o.log() // 5
const p = createObject()
p.log() // 42
Source by worcraft-algeria-dz.com #
 
PREVIOUS NEXT
Tagged: #function #How #JavaScript #closures
ADD COMMENT
Topic
Name
8+7 =