0

I have a static class in javascript...

var Logger = {
    logtofirefox: function (str, priority) {

         console.log(str);        

    },
    logtoie: function (str, priority) {

         alert(str);        

    }

}

I call it like this from another file...

Logger.log('Hello World');

But I want to inject the string 'log : ' before the words 'Hello World' on all functions.

Wondering if this is possible with minimum fuss?

1
  • From the given Logger object I'd say the call Logger.log('Hello World'); would throw a TypeError: Object #<Object> has no method 'log' Commented Feb 17, 2012 at 11:15

1 Answer 1

1

Just update the output for each function :

var Logger = {
    logtofirefox: function (str, priority) {
         console.log('log : ' + str);        
    },
    logtoie: function (str, priority) {
         alert('log : ' + str);        
    }
};
Sign up to request clarification or add additional context in comments.

2 Comments

This isnt really what I was looking for. You are duplicating code, what if I have multiple statements I have to then maintain multiple strings. I want to be able to do it all from one place. One static string....
what calls each function ? logtofirefox / logtoie ? you could prepend the 'log' string there ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.