jeudi 7 mai 2015

Javascript this keyword in return statement

I understand that the 'this' keyword is used to reference the object that the function is called upon, but i'm a bit unclear why you need 'this' when calling a function inside a return statement, but you do not need it when calling a function outside the return statement.

var module = (function() {
var privateFunction = function(){ }

return {
 publicFunction: function(){
   //the private function works the same with or without 'this'
   privateFunction();

   //but the public function doesn't work without 'this'
   anotherPublicFunction();
 },
   anotherPublicFunction: function(){}
}
});

module.publicFunction()




Aucun commentaire:

Enregistrer un commentaire