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 8931419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:06:45+00:00 2026-06-15T09:06:45+00:00

I am writing a library, and I have all functions being called with lib.someFunction();

  • 0

I am writing a library, and I have all functions being called with “lib.someFunction();” or “lib.someVariable;”. I know I shouldn’t pollute the global namespace, but I want to give users of my library that option. I heard some libraries have an “option” like “installLibrary();” that moves all functions and variables in a class to the global namespace.

This way, only users would prefer so would be able to call “someVariable;” or “someFunction();” directly.

My problem, though, is how to write such a function. Any ideas?

  • 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-15T09:06:46+00:00Added an answer on June 15, 2026 at 9:06 am

    You can do something like the following:

    function Library() { 
        this.var = 5; 
        this.otherVar = 4; 
        this.cool = function(x) {
            return x + this.otherVar;
        }
    } 
    var library = new Library();
    
    (function (lib, context) {
        for (var prop in lib) {
            if (lib.hasOwnProperty(prop) && prop) {
                if (typeof lib[prop] == "function") {
                    context[prop] = function() {
                        return lib[prop].apply(lib, arguments);
                    }
                } else {
                   context[prop] = lib[prop]; 
                }
            }
        }
    }(library, this));
    

    But beware of using anything other than functions here. If you do this and try to adjust parameters on the global/window namespace, they will not get carried back to your library functions and might not get the behavior you want. For example:

    library.cool(3);  // 7
    cool(3);          // 7
    otherVar = 10;
    library.cool(3);  // 7
    cool(3);          // 7  Still?
    library.otherVar = 10;
    library.cool(3);  // 13
    cool(3);          // 13  Finally!
    

    An alternative which doesn’t have this restriction would be to apply your library constructor function to the global/window object:

    Library.call(this);
    

    The trouble with that is that you don’t just pollute the global scope with your public API but also with anything that you might have stored in your local closure along the way. That strikes me as way too heavy.

    If your library uses something like the revealing module pattern and exposes only functions, then I think the first is pretty straightforward. But as others have said, it may not be wise. At the very most, I would offer this as an option for users but warn against it.

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

Sidebar

Related Questions

I am writing a library in C++ and have some functions that work with
I have been writing a library for my project (for now I am using
I have a library that I created with some business logic that includes writing
I am writing an iOS library to embed Lua in games and have encountered
I have been using Selenium Library with Robot Framework for writing automated tests for
I am writing a fairly large C++ shared-object library, and have run into a
I am writing a library which uses a few functions from the windows user32.dll
I am writing Perl scripts and when I have too many functions, I usually
I am writing a library for efficient number processing. I have to support different
I'm fairly new to C and have started out writing a small library with

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.