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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:54:20+00:00 2026-06-08T04:54:20+00:00

Say I have the following modules, split across multiple files both capable of extending

  • 0

Say I have the following modules, split across multiple files both capable of extending skillet:

File1.js:

(function(){
 var privateVar1 = 0;

 var privateFunction1 = function() {
  //function definiton
 };

 skillet.fry() = function() {
  //fry it
  //matchbox.light();
 };

})(window.skillet = window.skillet || {});

File2.js:

(function(){
 var privateVar2 = 0;

 var privateFunction2 = function() {
  //some private function 
 };

 skillet.grillIt = function() {
  //grill It
  //matchbox.strike(); <-- Shared with File1.js
 };

})(window.skillet = window.skillet || {});

Is it possible to have a shared variable/object like matchbox be sharable by the two modules without being bound to window.matchbox or window.skillet.matchbox? I.e. the visibility of matchbox should only be to File1.js and File2.js and must not be accessible elsewhere. I doubt if it’s possible, but is there a way to achieve such a behavior in JavaScript? If not, what’s the best practice to use in this regard?

(It’s more like having a shared event-bus among a set of related modules without exposing that bus globally)

  • 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-08T04:54:23+00:00Added an answer on June 8, 2026 at 4:54 am

    Nope.

    “private” variables work in JS only because of the scope that the function was declared in. There is no way to share that scope with a function declared in an entirely different scope. Scope is an unchangeable property of functions, once they are created.

    This is why this sort of thing is usually done with _foo style properties.

    skillet._matchbox = { strike: function() { ... } };
    

    The underscore prefix is convention for “internal” and serves as a hint not to mess with it.


    You could also get creative with how you pass matchbox around though, though in all cases it will mean providing a way to get matchbox out it’s original scope. Like perhaps, makes a skillet.extend method that passes the matchbox to it’s argument?

    (function() {
      var matchbox = { strike: function() { ... } }
      window.skillet = {
        extend: function(fn) {
          fn(matchbox);
        }
      };
    })();
    
    skillet.extend(function(matchbox) {
      var privateVar2 = 0;
      var privateFunction2 = function() {};
    
      skillet.grillIt = function() {
        //grill It
        matchbox.strike();
      };
    }
    

    Which allows you to use matchbox outside it’s original scope in a controlled way. But it also allows anyone to get matchbox that maybe shouldn’t.

    var stolenMatchbox;
    skillet.extend(function(matchbox) {
      stolenMatchbox = matchbox;
    });
    while (stolenMatchbox.count > 0) { stolenMatchbox.strike(); }
    alert("Now you are outta matches, sucker!");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following app.js (obviously very simplified): var express = require('express'),
lets say i have the following markup: <ul class=editor> <li> <a href=#>Modules</a> <a href=#>View</a>
Let's say I have the following function. let rec fib n = match n
It's common to see CommonJS modules defined using the following idiom: (function() { var
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
say I have the following <h3>This is my heading</h3> <p>This is my headingAnd this
Say I have the following chef role: name test description role for test run_list

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.