My Technical Notes

Thursday, 28 February 2013

Calling one function from another in the same Object

We use the this keyword:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Untitled Page</title>
    <script type='text/javascript'>
      myObj = { };
      
      myObj.function1 = function() {
       this.function2();
      };
      
      myObj.function2 = function() {
       alert('function2 called');
      };
    </script>
</head>
<body>
 <a href='javascript:void(0)' id='' onclick='javascript:myObj.function1();' class=''>call myObj.function1</a>
</body>
</html>

No comments: