Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Overriding a function

    Hello again, Forum:

    I have a question for ya. Is it possible to redefine a function in Javascript? For example:

    Code:
    function myTest(){
    
      this.showAlert = function(){
        alert('Show Me!')
      }
      this.callAlert = function(){
        this.showAlert();
      }
    }
    test = new myTest;
    test.showAlert = function(){ alert('No! Show Me Instead')}
    test.callAlert;
    Thank you!

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Give this a try

    PHP Code:
    function myTest(){

      
    this.showAlert = function(){
        
    alert('Show Me!')
      }
      
      
    this.callAlert = function(){
        
    this.showAlert();
      }
      
    }

    test = new myTest;
    test.showAlert = function(){ alert('No! Show Me Instead')}
    test.callAlert() 
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Or, considering that functions are objects which belong to the window element:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script type="text/javascript">
    function 
    myTest(){
    alert('Show Me!')
    }
    function 
    redefinemyTest(){
    window['myTest']= function(){alert('No! Show Me Instead')}
    }
    </script>
    </head>
    <body>
    <input type="button" value="Alert" onclick="myTest()">
    <br>
    <br>
    <input type="button" value="Redefine function" onclick="redefinemyTest()">
    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •