Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-28-2006, 06:03 PM   PM User | #1
Elcid32
New Coder

 
Join Date: Jul 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Elcid32 is an unknown quantity at this point
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!
Elcid32 is offline   Reply With Quote
Old 07-28-2006, 11:10 PM   PM User | #2
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
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.
Mr J is offline   Reply With Quote
Old 07-30-2006, 07:24 AM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:25 AM.


Advertisement
Log in to turn off these ads.