Register each method?

Instead of creating your own logger for TRACE, for example. following what methods were called and which classes were created, is there an easy way to make all the methods under the class log itself? This is for the node.js. application

class MyClass

  constructor: () ->
    console.log 'MyClass:constructor'

  doThat: () ->
    console.log 'MyClass:doThat'

exports.MyClass = MyClass

myClass = new MyClass()
myClass.doThat()

If I had my own path, you could see 4 log messages instead of 2 (so as to write less code to keep track of what is happening).

+5
source share
2 answers

- , OO- . , "", ; , , .

, :

Function::trace = do ->
  makeTracing = (ctorName, fnName, fn) ->
    (args...) ->
      console.log "#{ctorName}:#{fnName}"
      fn.apply @, args
  (arg) ->
    for own name, fn of arg 
      @prototype[name] = makeTracing @name, name, fn

, , @trace , :

class MyClass
  @trace methodA: ->
    @methodB 42

  @trace methodB: ->
    console.log "method b called with #{n}"

@trace :

class MyClass
  @trace 
    methodA: ->
      @methodB 42

    methodB: (n) ->
      console.log "method b called with #{n}"

, trace - CoffeeScript. method: -> 'foo' a class MyClass . @trace method: -> 'foo' trace MyClass ( Function, trace), method. JavaScript - this.trace({method: function() {return 'foo';}}).

trace ( ) () MyClass, , , .

(new MyClass).methodA() :

MyClass:methodA
MyClass:methodB
method b called with 42

, .

. , , , , ( , = D).


: , ​​- , : http://jsfiddle.net/2YuE7/ . move. move :

f = new Composite [
  new Rectangle 5, 10, 3, 4
  new Composite [
    new Circle 0, 0, 2
    new Circle 0, 0, 4
    new Circle 0, 0, 6
  ]
]

f.move 2, 3

:

(Composite[Rectangle[5,10,3,4],Composite[Circle[0,0,2],Circle[0,0,4],Circle[0,0,6]]]).move(2, 3)
| (Rectangle[5,10,3,4]).move(2, 3)
| -> Rectangle[7,13,3,4]
| (Composite[Circle[0,0,2],Circle[0,0,4],Circle[0,0,6]]).move(2, 3)
| | (Circle[0,0,2]).move(2, 3)
| | -> Circle[2,3,2]
| | (Circle[0,0,4]).move(2, 3)
| | -> Circle[2,3,4]
| | (Circle[0,0,6]).move(2, 3)
| | -> Circle[2,3,6]
| -> Composite[Circle[2,3,2],Circle[2,3,4],Circle[2,3,6]]
-> Composite[Rectangle[7,13,3,4],Composite[Circle[2,3,2],Circle[2,3,4],Circle[2,3,6]]]
+3

- javascript, , , API, . polyfill, http://jsfiddle.net/mcgraphix/pagkoLjb

:

var api = {
   notAMethod: "blah",
   foo: function() {
    console.log("in foo", arguments);
   },

   bar: function(arg) {
    this.foo(arg);
    return this.notAMethod;
   }
}; 

//wrap the api with a logging version
//to watch property changes and keep them in sync, you need the polyfill for Object.watch
//     from https://gist.github.com/eligrey/384583

var createLogger = function(api) {
     var loggingApi = {};

            for (var prop in api) {
                if (typeof api[prop] !== 'function') {
                   loggingApi[prop] = api[prop]; 
                   loggingApi.watch(prop, function(prop, oldVal, newVal){
                       api[prop] = newVal
                       return newVal;
                   });


                } else {

                    loggingApi[prop] = function() {
                        console.log(prop + "() called with args: ", arguments);
                        var returnVal = api[prop].apply(api, arguments);
                         console.log(prop + "() returned: " + returnVal);
                        return returnVal;
                    }
                }
            }
      return loggingApi;
 };
 api.foo('Shhhh... don\'t log me') //no logging
 createLogger(api).foo(); //with logging           
0

All Articles