CopyAndPaste

random programming notes

Javascript Instance Method This Variable

In Javascript class instance method, you must use the this variable to access instance variables. For instance:

Javascript version
1
return this.foo;

as oppose to in C++:

C++ version
1
return foo;

One way to get around with using this for every instance variable is as follow:

1
2
3
4
5
MyClass.prototype.myFunc = function() {
  with(this) {
    return foo;
  }
}