What is a best practice for ensuring “this” context in Javascript?
问题 Here's a sample of a simple Javascript class with a public and private method (fiddle: http://jsfiddle.net/gY4mh/). function Example() { function privateFunction() { // "this" is window when called. console.log(this); } this.publicFunction = function() { privateFunction(); } } ex = new Example; ex.publicFunction(); Calling the private function from the public one results in "this" being the window object. How should I ensure my private methods are called with the class context and not window?