Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > how to override a function?

Reply
Thread Tools

how to override a function?

 
 
mitch
Guest
Posts: n/a
 
      04-26-2006
Is there a way to reassign a function? I tried this:

function func() { alert("func"); }
function func2() { alert("func2"); }

function testFunc() {
document.func = func2;
func();
}

and it prints "func" not "func2". I'd like to be able to override the
global function "func" to make it do something else. Is there a
trick to do it? Thanks.

The reason I want to do it is for unit testing. I have some test
code I'm writing, and I want to call the production code and override
some of the functions in the production code, making other
functions unit-testable. I would prefer to do it without changing
the production code.

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      04-26-2006


mitch wrote:

> Is there a way to reassign a function? I tried this:
>
> function func() { alert("func"); }
> function func2() { alert("func2"); }
>
> function testFunc() {
> document.func = func2;

window.func = func2;
> func();



--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
TheBagbournes
Guest
Posts: n/a
 
      04-26-2006
mitch wrote:
> Is there a way to reassign a function? I tried this:
>
> function func() { alert("func"); }
> function func2() { alert("func2"); }
>
> function testFunc() {
> document.func = func2;
> func();
> }
>
> and it prints "func" not "func2". I'd like to be able to override the
> global function "func" to make it do something else. Is there a
> trick to do it? Thanks.


func = func2 will do it.

Of course then you've lost the original func function.

(The default owner object is window, not document, so window.func =
func2 means the same thing)

ExG
 
Reply With Quote
 
mitch
Guest
Posts: n/a
 
      04-26-2006
Thanks, window.func worked.

- Mitch

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      04-27-2006
mitch said on 27/04/2006 2:35 AM AEST:
> Is there a way to reassign a function? I tried this:
>
> function func() { alert("func"); }
> function func2() { alert("func2"); }
>
> function testFunc() {
> document.func = func2;
> func();
> }
>
> and it prints "func" not "func2". I'd like to be able to override the
> global function "func" to make it do something else. Is there a
> trick to do it? Thanks.
>
> The reason I want to do it is for unit testing. I have some test
> code I'm writing, and I want to call the production code and override
> some of the functions in the production code, making other
> functions unit-testable. I would prefer to do it without changing
> the production code.


In addition to other responses, functions declared in the global scope
belong to the global object. In browsers, the global object is
synonymous with the window object.

However, not all user agents are browsers and therefore may not have a
window object. To be confident of explicitly addressing global objects
and properties, you may want to create your own reference to the global
object (usually at the beginning of the script):

var _global = this;


Now you can use:

function testFunc() {
_global.func = _global.func2;
_global.func();
}


In some situations that may be good practice, but usually:

function testFunc() {
func = func2;
func();
}


will do.


--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
prevent VTP override by rogue switch on access switchport... wr Cisco 6 09-07-2004 04:16 AM
protected override void CreateChildControls() Jon ASP .Net 4 08-07-2003 08:16 PM
Using inheritance for web pages - why have a Page_Init when you can override OnInit? ASP .Net 2 07-16-2003 07:32 PM
Using inheritance for web pages - why have a Page_Init when you can override OnInit? ASP .Net 2 07-13-2003 04:07 AM



Advertisments