A local variable is just that, "local" to the function it's declared in. You can think of it as though each function call getting a new copy, so for example:
function feedPig()
{
var pig;
console.log(pig);
pig = 812;
console.log(pig);
}
feedPig();
feedPig();
Output:
undefined
812
undefined
812
Whereas
this refers (
usually--it's a bit thornier in JS compared to, say, C++) to the object of a method. Anything stored on
this gets carried around with the object. Therefore:
class PigFeeder
{
feedPig()
{
console.log(this.pig);
this.pig = 812;
console.log(this.pig);
}
}
var obj = new PigFeeder();
obj.feedPig();
obj.feedPig();
Output:
undefined
812
812 <-- note the difference from above
812