In the first case, you just get a link to a function on vairable hello and calling it from the global context (window in browsers, global in node), So this becomes what called the function, except for (related functions). You can always explicitly specify the context using function.call or explicitly setting the context of the function using Ecma5 function.bind
hello.call(myNamespace.myObject);
or just bind it when you get a function reference.
var hello = myNamespace.myObject.sayHello.bind(myNamespace.myObject); //Now no matter where you call it from `this` will point to the context of myObject
The second case, you call it from the object itself, so this points to the object.
source share