Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Debugging Pattern: "ControllerID"
Connor Dillon
Connor Dillon

Posted on

     

Debugging Pattern: "ControllerID"

This debugging pattern was shared with me by a close friend (who also happens to be a very skilled developer). This is an incredibly useful and time-saving debugging pattern when working with modules or controllers that need to be instantiated only once per instance.

The idea behind this pattern is to apply a controller ID to each instance of a given module/controller in order to keep track of the number of times it is being instantiated.

To do this, create a global variable that we will increment:

let controllerCount = 0
Enter fullscreen modeExit fullscreen mode

Then, assign a variable within the module that will be assigned a controller ID, and increment the controller count:

export const Controller = () => {    const controllerID = ++controllerCount}
Enter fullscreen modeExit fullscreen mode

Then, add the following code to your tests:

console.log({data, controllerID})
Enter fullscreen modeExit fullscreen mode

Create Custom DOM & Window Tests

To make information that would normally be buried deep within the DOM, we can create our own tests to make things more readily available.

Just add this pattern to the correct scope:

window.test = { module1, module2, ... }
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Full-stack Javascript developer, fire dancer, physics nerd, Vim fanboy, and technical writer! I'm passionate about my work and love to talk shop, so say hi!
  • Location
    Seattle, WA
  • Work
    Software Engineer
  • Joined

More fromConnor Dillon

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp