• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

182
Views
JavaScript closure question with counter example

Consider we have follow snippets:

function Counter() {
  let count = 0
  
  function inc() {
    count += 1
  }
  
  function dec() {
    count -= 1
  }
  
  function getCount() {
    return count
  }
  
  return { getCount, inc, dec, count }
}

const counter = Counter()
counter.inc()
counter.inc()

console.log(counter.getCount()) // 2
console.log(counter.count) // 0

I am wondering why using function getCount() and directly return count variable shows different result. In my opinion they both reference the same count address, and after inc() calls its value should be altered accordingly.

almost 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

return { getCount, inc, dec, count }:

  1. Creates a new object
  2. Copies the current values of those four variables onto properties with the same names
  3. Returns that object

When you change the value of the count variable you do not change the value of the count property.

almost 3 years ago · Juan Pablo Isaza Report

0

The reason is because when you do counter.count, you are getting the count value off the of the new object you returned. That count is actually a copy of the value at the time it was created and will never get updated.

If instead, you create a class and use this.count instead, it'll always be updated.

class Counter {
  constructor() {
    this.count = 0;
  }
  
  inc() {
    this.count += 1;
  }
  
  dec() {
    this.count -= 1;
  }
  
  getCount() {
    return this.count;
  }
}

const counter = new Counter();
counter.inc()
counter.inc()

console.log(counter.getCount()) // 2
console.log(counter.count) // 2

Or, if you want to do it the more old-school way:

function Counter() {
  this.count = 0
  
  function inc() {
    count += 1
  }
  
  function dec() {
    count -= 1
  }
  
  function getCount() {
    return count
  }
  
  this.inc = inc;
  this.dec = dec;
  this.getCount = getCount;
  
  return this;
}

const counter = Counter()
counter.inc()
counter.inc()

console.log(counter.getCount()) // 2
console.log(counter.count) // 2

Or, you could make count and object with some value that would get updated:

function Counter() {
  let count = { value: 0 }
  
  function inc() {
    count.value += 1
  }
  
  function dec() {
    count.value -= 1
  }
  
  function getCount() {
    return count.value
  }
  
  return { getCount, inc, dec, count }
}

const counter = Counter()
counter.inc()
counter.inc()

console.log(counter.getCount()) // 2
console.log(counter.count.value) // 2

Though that last one is a bit silly for this particular use-case.

almost 3 years ago · Juan Pablo Isaza Report

0

Mostly because count that you are returning from function is a copy of variable (value to be more accurate) and getCount is a function, which refers to reference of a count variable. More you can learn here.

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error