Assign JavaScript function as variable and call it - Javascript Function

Javascript examples for Function:Function Variable

Description

Assign JavaScript function as variable and call it

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//  ww  w.ja  va  2s.co m
var o = {};
o.name = 'foo';
var f = function() {
    return this.name;
}
o.someFunction = f;
var result = o.someFunction();
document.getElementById("test").innerHTML = result;
    }

      </script> 
   </head> 
   <body> 
      <div id="test">
         This text will change
      </div>  
   </body>
</html>

Related Tutorials