-3

Why can't we bind this like here

function UserCreator(name, score) {
  this.name = name;
  this.score = score;
}

UserCreator.prototype.increment = function () {
  function add1() {
    this.score++;
  }

  this.add1()
};

UserCreator.prototype.login = function () {
  console.log("login");
};

const user1 = new UserCreator("Eva", 9)
user1.increment()

returns "this.add1 is not a function". Of course, I can do it directly, but sometimes you need sub methods and I want to understand how this works

Makyen
  • 27,758
  • 11
  • 68
  • 106
user1076808
  • 29
  • 1
  • 7
  • Can you please share what are you trying to achieve? :) – John Samuel Oct 25 '19 at 08:14
  • Your code is also a mess. You need to intend it properly and it would be a lot easier to understand. It would also help if you provided a live demo. The Stackoverflow question editor has a button for making live demos, which includes a feature to automatically fix your indentation. Use it. – Quentin Oct 25 '19 at 08:14
  • 2
    Why do you decalre a function in a function which will be directly called? Just add `this.score++;` in the `UserCreator.prototype.increment` function withou an other function. – thmspl Oct 25 '19 at 08:14
  • if that code were in strict mode, the error would be `this is undefined` ... along those lines anyway – Bravo Oct 25 '19 at 08:16
  • when you call `add1` the value of `this` is not `user1` - there's a question about how `this` works on stackoverflow - someone should find it and dupe this – Bravo Oct 25 '19 at 08:17
  • 1
    @Bravo https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work I can't vote for a dupe, but this is the dupe I believe you are referencing. – Daedalus Oct 25 '19 at 08:20
  • Check out the methods Functions provide. In particular [call](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) and [apply](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) – Thomas Oct 25 '19 at 08:35
  • @Daedalus - that's the one – Bravo Oct 25 '19 at 08:38
  • So I think what's happening is that due to this being referring to windows. Code is running Windows.add1() and if just directly call add1() program should run Windows.score++ which is NaN – user1076808 Oct 25 '19 at 08:45
  • @user1076808 [`window`](https://developer.mozilla.org/en-US/docs/Web/API/Window), not Windows. But to fix it simply replace `function add1()` with a proper prototype method like you have the others. I can't say specifically why this is, from multiple reasons stretching from "it's late" to "I'm tired", but that should fix it, roughly. I'm not going to add this as an answer. Someone more knowledgeable can do that. – Daedalus Oct 25 '19 at 08:47

1 Answers1

0

So essentially its running window.add1() and if called using add1() it would run window.score++ returning a NaN. Need to work on my "this" concepts.

user1076808
  • 29
  • 1
  • 7
  • 2
    At the point that you are calling `this.add1()`, `this` !== `window`. I suggest you use `console.log()` and/or the debugger in order to explore what is going on. – Makyen Oct 26 '19 at 00:06