...

View Full Version : Overriding a function



Elcid32
07-28-2006, 06:03 PM
Hello again, Forum:

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



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!

Mr J
07-28-2006, 11:10 PM
Give this a try


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()

Kor
07-30-2006, 07:24 AM
Or, considering that functions are objects which belong to the window element:


<!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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum