0
let a=new A();
let b=A();

How can I write the function constructor in such a way that the following values are returned?

console.log(a) \\ return 1
console.log(a) \\ return 2
console.log(a) \\ return 3

Following is my code

function A() {
  this.count=1;
  return function() {
    return this.count++;
  }
}

It works using closures but only when console.log(a()) is called 3 times and not console.log(a).

  • If you're using `this` I don't think you can call it like a normal function. – evolutionxbox Apr 08 '21 at 09:24
  • 2
    [What is the problem you're trying to solve?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) Although, this seems like an interview question, in which case - how are we supposed to give you the answer when it's up to the interviewers what's "correct". – VLAZ Apr 08 '21 at 09:26
  • even if i replace this.count with count . Still the closure returns a function. I need the output when i call console.log(a). – Seema jain Apr 08 '21 at 09:26
  • 2
    Possibly related: [Can (a== 1 && a ==2 && a==3) ever evaluate to true?](https://stackoverflow.com/q/48270127) – VLAZ Apr 08 '21 at 09:27
  • No, it's not an interview question. I was learning this and closures and this was part of an assignment on the course. I have spent over an hour on this . – Seema jain Apr 08 '21 at 09:29
  • `a` is a function. If you want the internal count variable to increment, then it needs to be called each time. `a()` – evolutionxbox Apr 08 '21 at 09:30
  • Are you sure this is correct? Because it doesn't seem related to closures. Not as something you'd teach other. It's one of the things you'd have pour cement over and pave over after you've learned it, lest it escapes into any real-world code. – VLAZ Apr 08 '21 at 09:30
  • This looks more like a misinterpreted assignment about currying. https://jsfiddle.net/4mowtvn8/1/ – Ivar Apr 08 '21 at 09:34
  • I mean, it is possible, like in [this answer](https://stackoverflow.com/a/48270332/4642212) of the question linked above: `{ let x = 0; Object.defineProperty(globalThis, "a", { get(){ return ++x; } }); }`. Then `console.log(a); console.log(a); console.log(a);` will print `1`, `2`, `3`. – Sebastian Simon Apr 08 '21 at 09:34
  • @SebastianSimon [meh, it's even easier](https://jsbin.com/pamasec/edit?js,console). But not something I'd class under "closures". – VLAZ Apr 08 '21 at 09:39
  • @VLAZ But the browser’s `console.log` doesn’t stringify by default. Sure, replacing `console.log` (as is done on JSBin) by a function that stringifies its arguments is also possible. – Sebastian Simon Apr 08 '21 at 09:42
  • @SebastianSimon depends where this is run, yes. It was a goofy example to highlight how much of not-a-closure this task is. – VLAZ Apr 08 '21 at 09:44
  • @SebastianSimon when i run it on a browser it returns a function. – Seema jain Apr 08 '21 at 09:44
  • @Seemajain The `Object.defineProperty` snippet? It doesn’t log or return a function anywhere, neither on Chrome nor on Firefox. If you mean the `toString` approach: yes, that’s why it requires stringification. – Sebastian Simon Apr 08 '21 at 09:47

0 Answers0