• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AOP in javascript

 
Trilochan Bharadwaj
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Eric Pascarello
    author
    Rancher
    Posts: 15385
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 100
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic