DEV Community

Cover image for ToolJS: Enabling Code Debugging
Okoro Redemption
Okoro Redemption

Posted on

ToolJS: Enabling Code Debugging

ToolJS: Code Debugging

In the previous post, we introduced ToolJS and saw how to install it into a work environment.

In this post, we'll see the code debugging capabilities of ToolJS. This will be the shortest post in the series.

ToolJS Config

The ToolJS default export offered for node, es6 users and the ToolJS namespace in the window object for browser users, offers a set of methods which helps the use of the class. We'll be talking about the ToolJS.config() method.

The ToolJS.config method offers us the ability to configure our ToolJS working environment. This configuration at the moment of this post includes code debugging.

There are useful console logs and warnings that might come in handy during development, but not so much for production. With that said, ToolJS offers a configuration option that disables and enable the debugging of your ToolJS code. Let us see how its done.

// use the config method of the ToolJS class to set debugging to true
ToolJS.config({ debugging: true });
Enter fullscreen mode Exit fullscreen mode

The above code will enable logs to your console during your execution process.

Lets see an example..

Consider the follow html

<p>I am a p tag with no id</p>
Enter fullscreen mode Exit fullscreen mode

And then the following javascript

// use the config method of the ToolJS class to set debugging to true
ToolJS.config({ debugging: true });

// export the DOM module's methods
var $ = ToolJS.export("DOM");

// the below code will fail, because there is no element in the DOM matching the selector
// this will output a log to the console

// trying to get an with to get an element from the DOM with id = "pTag"
var myPara = $.getEl("#pTag");

Enter fullscreen mode Exit fullscreen mode

Lets try another example using one of the methods in the Calc module

// export all the methods in Calc module
var MyCalc = ToolJS.export("Calc");

// the add method only accepts numbers or array of numbers as a parameter,
// so passing it a string would output a warning to the console.
MyCalc.add(4, 4, "Okoro");
Enter fullscreen mode Exit fullscreen mode

See the Pen ToolJS: Code Debugging by Redemption Okoro
(@akaawereniya) on CodePen.

This feature can come in handy when you have bugs, this makes it easier to know what the bug might be.

Conclusion

Debugging is always very useful when coding, thats why this feature was included into the ToolJS library to help debug your ToolJS code

Top comments (0)