Classes in ES6 are just syntactic sugar over prototypal inheritance. This way the method declarations in the class are attached to the prototype object.
1) The problem in the example is in the way the function is invoked. To keep the context this, you still need to invoke it as a method on an object:
var foo = new Foo();
var executing = function (someMethod, context) {someMethod.apply(context)};
executing(foo.methodTwo, context);
2) But if you invoke it as a regular function, then you'll have this as undefined in strict mode:
methodTwo() {
console.log("MethodTwo is called");
this.methodOne(); // <---- `this` is `undefined`
}
this in a function call is determined by the way it is invoked:
- as regular function (makes
this as undefined or global object in non strict mode): someMethod(); or var m = foo.methodTwo; m();
- as a method on an object (makes
this the object): foo.methodTwo()
- as constructor (makes
this the newly created object): new Foo()
- indirect invocation (using
apply(newContext) and call(newContext)): someMethod.apply(context).
Notice that bound() method can modify the context before invocation. It will cancel any later context modifications on invocation for cases 1, 2 and 4. For constructor invocation the bound context is ignored and still used the newly created object.
Check this nice post about the function context and invocation in JavaScript.