0

A function, such as below, may have a local function defining this.inc. Changing the value inc to any other, such as say i, changes the result - so clearly, this .inc is not a user defined variable, but something else internal to this. Is there any explanation as to what inc is, and why it functions like so?

function incr () {
  var i = 0;

  this.inc = function() {
    i++;
  }
}

This change in result can be observed in an answer to my previous question, found here, such as in this construction (Thanks to Paul S for expanding it from a Ternary Operator):

if (total !== 0) {this.inc = (this.inc || 0) + 1;} else {this.inc = -1 +1;}

So, to reiterate, changing this.inc to this.i (or any other variable presumably) does not work, but this.inc does work. So, what exactly is this.inc?


Due to my tiredness and incorrectly testing this.inc before posting this question, I misunderstood the usage of the variable inc as being internal to this, rather than miscellaneously defined by the programmer, since changing it was not initially working for me. Due to this, this question is requested for deletion.

user1679669
  • 342
  • 5
  • 17
  • 2
    You're sort of right, but your wrong in that a ternary does not introduce any new function/scope/closure/context, which is why the behaviour is different. Rather than expanding out the RHS, and leaving it equal to the LHS, move the LHS into each branch of the RHS, then expand, and you should see how it works `if (total !== 0) {this.inc = (this.inc || 0) + 1;} else {this.inc = -1 +1;}` – Paul S. Feb 21 '17 at 00:09
  • @PaulS. Cheers, that works. – user1679669 Feb 21 '17 at 00:35
  • Although not a duplicate per se, `this` is explained answers to http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback/20279485#20279485 (and linked questions) – user2864740 Feb 21 '17 at 00:40
  • @user2864740 It appears in my tiredness yesterday, my question was incorrect in that changing "this.inc" to another variable, say, "this.i" would not work - because now, it does work. Apparently, "inc" was not internal to "this" at all. I already understand the concepts you linked to, but am now unsure whether to request my question for deletion or not - but thank you for link nonetheless. – user1679669 Feb 21 '17 at 17:16

2 Answers2

0

In your example, inc is a property of the function incr. Since everything in javascript is an object, any function can have it's own properties just like any other object. this is a special javascript reference which references the current object, and the dot operator is a way of referencing a property of the given object, in this case, the object's self.

JakeParis
  • 11,056
  • 3
  • 42
  • 65
0

It appears that this.inc is supposed to be used as a static variable. This means that the next time you call "progress()" function, the previous value of ".inc" must still remain. But I find this inappropriate. It's better to define "var inc" outside the progress() function such as the following:

var inc = 0;

function progress(total) {
  inc = (total !== 0? inc || 0: -1) + 1;
  var percentage = Math.floor(inc / total * 100);
  document.write('Processing ' + inc + '/' + total + ' ' + percentage + '%<br/>');
}

Technically, a javascript function is also an object. So within the function call, you can assign a property for the function using "this". So by declaring "this.inc" inside it, the function will now possess a property called "inc"

Ronald Borla
  • 586
  • 4
  • 19
  • this is not a good solution, you can improve the original, but not like this – Keith Nicholas Feb 21 '17 at 00:47
  • @KeithNicholas Is there any reason why this is so? – user1679669 Feb 21 '17 at 00:55
  • you've isolate the function from the dependent variable – Keith Nicholas Feb 21 '17 at 00:57
  • @KeithNicholas Thanks, thought as much (didn't test it). – user1679669 Feb 21 '17 at 00:58
  • @KeithNicholas If you look at the "progress()" function in the link OP provided, progress() is obviously called only as a function and not declared as a new object. There's no reason to use "this" inside a function as this might confuse the programmer into thinking that the function is used as a constructor of a new object. Clearly OP got confused of the "this" – Ronald Borla Feb 21 '17 at 01:02
  • @RonaldBorla Yes, I was definitely confused although I understand it was a new 'instance' of the called iteration (so to speak). Your answer does also work in place of the original (even if it could be improved). – user1679669 Feb 21 '17 at 01:28
  • that is better, but the whole point is to not confuse beginners.. it's not hard to isolate your codes, but anyway, isolation is for a different topic – Ronald Borla Feb 21 '17 at 01:56
  • @RonaldBorla It appears in my tiredness yesterday, my question was incorrect in that changing "this.inc" to another variable, say, "this.i" would not work - because now, it does work. Following your comment made me realize this is no internal sorcery, but instead a failing of my tiredness and poor testing before posting my question. I already understand the concepts you spoke of in your answer however, and am unsure whether to request my question for deletion or not - but thank you for your explanation nonetheless. – user1679669 Feb 21 '17 at 17:13