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

  • Home
  • SEARCH
  • 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 8691491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:07:53+00:00 2026-06-13T00:07:53+00:00

Lets say I have a javascript library (.js file) which does something like drawing

  • 0

Lets say I have a javascript library (.js file) which does something like drawing an SVG graph on a div element that is on a page. The library applies business logic and also creates event handlers for when elements are clicked etc. Pseudo-code could look like this:

myLib = {};
myLib.dummy = null;
mylib.doStuff = function(divID) {
  //divID is the ID of the element on which SVG graph will be drawn
  //does graph drawing and sets up myLib.dummy for feedback or such, e.g.:
  myLib.dummy = $("#divID").width();
}

My question is: how do I handle a case where I want to apply this script to 2 (or more) elements on the same page, and then have seperate “instances” of myLib pertaining to each element. I know I cannot do call it like this from the page:

myLib.doStuff('topDiv');   //myLib.dummy will be say 400;
myLib.doStuff('bottomDiv');   //myLib.dummy will be say 600;

With the above code, I will no longer have access to myLib.dummy that pertains to div with ID = ‘topDiv’, since they are using the same namespace.

I suspect I am missing a design pattern or that the answer could be simply a case of namespacing.

I would appreciate any suggestions or help.

  • 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-13T00:07:54+00:00Added an answer on June 13, 2026 at 12:07 am
    function makeALib() {
        var myLib = {};
        myLib.dummy = null;
        mylib.doStuff = function(divID) {
          //divID is the ID of the element on which SVG graph will be drawn
          //does graph drawing and sets up myLib.dummy for feedback or such, e.g.:
          myLib.dummy = $("#divID").width();
        }
        return myLib;
    }
    
    var libA = makeALib();
    var libB = makeALib();
    
    libA.doStuff('topDiv'); 
    libB.doStuff('bottomDiv'); 
    

    There’s many different areas where you could introduct the seperation or indirection. Without knowing more about your usage context, its hard to say what’s a good choice. But the above is one of many working choices.

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

Sidebar

Related Questions

How can I instantiate an existing div element using javascript? Lets say I have:
Lets say I have a javascript function call moveItem which takes two parameters source
I'm having some Javascript woes, lets say we have a constructor function test(element) {
Lets say I have a JSON document / JavaScript object that looks like this:
Lets say you have a JavaScript widget which needs to fire off a request
I have a javascript variable which hold the value taken from somewhere else(lets say
Lets say I have a Javascript object: var myObj = { property1 : 20,
Lets say I have the following array in JavaScript: var skins = new Array('Light',
Lets say I have an Android html5 app with a picture generated with Javascript
Let's say I have some simple Javascript like: <script> var hello = function(){ alert(Hello

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.