Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8184357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:28:28+00:00 2026-06-07T01:28:28+00:00

I’m working on a somewhat large-scale JS application and I want to break out

  • 0

I’m working on a somewhat large-scale JS application and I want to break out some common functions into a utility class. I’m not quite clear on how I should go about it, however.

For example, say I have a bunch of shape-based classes that need to call a function in the utility class:

// Triangle class
Triangle.prototype = new Shape();
Triangle.prototype.constructor = Triangle;

function Triangle() {...}
Triangle.prototype.drawOutline = function() {
  var outline = Utility.canvas.trace(this._canvas); // Trace the shape.
  this._viewport.drawImage(outline, 0, 0); // Draw the traced outline.
}

// Circle class
Circle.prototype = new Shape();
Circle.prototype.constructor = Circle;

function Circle() {...}
Circle.prototype.drawOutline = function() {
  var outline = Utility.canvas.trace(this._canvas); // Trace the shape.
  this._viewport.drawImage(outline, 0, 0); // Draw the traced outline.
}

Does it make sense to instantiate the Utility class at the global level? I feel like there should be an additional level above it, something like Application so I’d have Application.Utility, Application.UI, and so on. Unfortunately, going this route forces each specialized shape class to know what the greater program structure is (so it can reference Application.Utility), which isn’t very good.

Is there a better, preferred manner to have specialized JS classes make use of generalized class functions that aren’t inherited?


Edit: It might help to say that I’m trying to relate this to an #include or import statement from C++ or Java. What’s the best way to go about this in JS?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:28:30+00:00Added an answer on June 7, 2026 at 1:28 am

    Assuming that you’re wrapping all your code in an anonymous function then what I usually do is just create an object (namespace) and put all the utils there:

    ;(function(){
    
      var Utils = {
        triangle: function () { ... },
        circle: function () { ... }
      }
    
    }())
    

    Then whenever you use it you just do Utils.circle(...) that way you know when you’re using utils and it’s easy to keep track of them, add new ones, etc.

    It might help to say that I’m trying to relate this to an #include or
    import statement from C++ or Java. What’s the best way to go about
    this in JS?

    In this case the simplest way is to create a concat.sh file or similar and a start.js and end.js and add everything in between.

    start.js

    ;(function(){
    

    utils.js

    var Utils = {
      triangle: function () { ... },
      circle: function () { ... }
    }
    

    end.js

    }())
    

    concat.sh

    Order is important, of course.

    #!bin/bash
    cat start.js utils.js end.js > out.js
    

    If you’re into node.js check out grunt that does all of this for you with little configuration.

    Otherwise, for more advanced stuff use CommonJS modules.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.