0

I have a constructor that has a series of classes and functions within it - in each function I check if the param is set to show the error and output if so. Like so using an inline if. The issue and question is short of keeping two versions is this approach not wise in that each IF has to be evaluated thus adding to the time it takes to perform?

debugSlide =  (bag.debug==1)? console.log("item id =" + bag.itemId) : 0;

How do you do this? Any pointers on where I can find tips on this? Thanks in advance!

1
  • if(console) console.log('in debugmode'); Commented Mar 8, 2010 at 19:13

1 Answer 1

1

This is exactly the type of problem that polymorphism is good at.

var SomeObject = function( initialDebugger )
{ 
  this.test = function()
  {
    alert( 'Test!' );
    this.debugger.log( 'Executing SomeObject.test()' );
  }

  this.setDebugger = function( newDebugger )
  {
    this.debugger = newDebugger;
  }

  if ( 'undefined' == typeof initialDebugger )
  {
    initialDebugger = new SilentDebugger();
  }
  this.setDebugger( initialDebugger );
}

var SilentDebugger = function()
{
  this.log = function(){}          
}

var FirebugDebugger = function()
{
  this.log = function()
  {
    console.log.apply( window, arguments );
  }
}

var sample = new SomeObject();

sample.test();
sample.setDebugger( new FirebugDebugger() );
sample.test();
Sign up to request clarification or add additional context in comments.

Comments

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.