The Artima Developer Community
Sponsored Link

Java Buzz Forum
Coderefs in Javascript

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Chris Winters

Posts: 931
Nickname: cwinters
Registered: Jul, 2003

Daytime: Java hacker; nighttime: Perl hacker; sleeptime: some of both.
Coderefs in Javascript Posted: Mar 26, 2004 7:08 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Chris Winters.
Original Post: Coderefs in Javascript
Feed Title: cwinters.com
Feed URL: http://www.cwinters.com/search/registrar.php?domain=jroller.com®istrar=sedopark
Feed Description: Chris Winters on Java, programming and technology, usually in that order.
Latest Java Buzz Posts
Latest Java Buzz Posts by Chris Winters
Latest Posts From cwinters.com

Advertisement
This may be old hat to you, but I didn't know it. JavaScript since 1.2 has had the equivalent of Perl code references, or function pointers, that you can pass around and execute as needed. Among other things, this makes it easy to create a generic library while customizing it with a callback:
var aCallback = null;
function registerCallback( cb ) {
    aCallback = cb;
}
function doSomeLibraryStuff( foo, bar ) {
    // do some stuff
    if ( aCallback != null ) {
        aCallback( foo, bar );
    }
}

So in your library you can just register your custom functionality and it gets executed whenever the doSomeLibraryStuff function is run:

registerCallback( function( foo, bar ) {
    if ( foo < bar ) {
        alert( "All your base" );
    }
});

You can also use a Function object for this, but the constructor uses a list of Strings that get joined with a ';' and eval'd into existence. Ugly. And these support closures as well.

Read: Coderefs in Javascript

Topic: Referrer spam comes to pbblog Previous Topic   Next Topic Topic: Java Look and Feel Design Guidelines

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use