Medium Feed

 

Tuesday, April 21, 2015

Accessing Self execiting / anonymous functions objects from Ouside

As we all know the purpose of self execution function is to call setup codes automatically without any caller functions or event calls.

Here is the sample self execution function :

(function  (){
   function RamCool ()
     {
     alert  ('Cool');
      }
})();

In the above example we have created "RamCool " function inside the self executing function pattern and if you want to call that function from outside like below ..Your will get Error

function  (){
   function RamCool ()
     {
     alert  ('Cool');
      }
})();

<button id="sample " onclick="RamCool ();">

Because we can not make a call from outside to a function defined in self executed function pattern


SOLUTION:  How to handle this ?

We should follow the approach of Jquery library.  I mean we have to create as global variable or extend the existing global object.  Like below :

function  (){
  Var foe= function ()
     {
      }:

  foe.RamCool=function  (){
  alert ( ' Cool ' );
};
 return (window.foe=foe);
})();

<button id="sample " onclick="foe.RamCool ();">

Thats it !! You are done !. Now you should be able to "RamCool " from outside anywhere. 

No comments:

Post a Comment