This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes AOP in javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AOP in javascript" Watch "AOP in javascript" New topic
Author

AOP in javascript

Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
How do I add logs to before / after / around on a function call, I figure there has to be a way by using Function.prototype.call = function () { }, or Function.prototype.apply = function () { }, but I can't figure out:
A.) Once I do my logging, How do i call the original function back?
B.) Is there another way of intercepting it? I looked up at Object.watch( ) but I cant be sure if its going to be useful

Any suggestions?

Trilochan.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Well call and apply are not good names to use because of call and apply

So you basically want to override the original

You should be able to do this





Eric
Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
Eric,
Thanks for your response.

OK, cool I understand, But this is more or less a static intercept, if you know what I mean, What if I wanted to do dynamic intercepting, like, every time a function with name that starts with 'get*' is called, I would log it, else execute it as is without logging;

In that case, would I want to override the original call or apply function? if so any pointers ?


Trilochan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

Hmm, not something I've ever thought of but off the top of my head:

  • Traverse the properties of the window looking for functions
  • Replace the function with one that includes the function as a closure
  • The new function will do pre and post processing stuff around calling the closured function


  • Of course, the above would only work for top-level functions. Methods of objects are harder to find, but you could apply the above to them one by one.

    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Eric Pascarello
    author
    Rancher

    Joined: Nov 08, 2001
    Posts: 15376
        
        6
    Basic idea



    You can make addGetLogging more configurable if you can pass in what you are looking for and if it is a exact match or not.

    Eric
    Trilochan Bharadwaj
    Ranch Hand

    Joined: Feb 02, 2009
    Posts: 100
    Eric, Bear, thanks for your help guys!

    Good deal; I'll try it out and post stuff here; I did try this however based on my limited knowledge:



    Now, I may be a noob, but I do know that using eval === bad smell in code, so I'll skip this solution. In that code, anyways, only getX( ) function gets invoked with before and after, but can't figure out why others (getY(), getZ( ) are not), also Why is arguments coming up as the whole function?!! is that because I wrote the function in a single line?



    Trilochan.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: AOP in javascript