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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:22:38+00:00 2026-06-12T18:22:38+00:00

If I copy code into a closure like this (function(){ var private1 = {},

  • 0

If I copy code into a closure like this

(function(){
    var private1 = {},
        private2 = {},
        publik;

    publik.get(object){
         private1[object.name] = object;
    };

    window.publik = publik;
}())


publik.get({
    // API
    name: 'name_foo',
    functionA: function(){
        // I can not access private2 in here
    },
    functionB: function(){}
})

Is there a way the API can work, so that the functionA and functionB can have access to private 2 inside the closure.

They will be copied into the closure, so the copied version should have access to it. That is why I made the closure and put private1 and private2 together like that.

The written version ( not the copied version ) of the object throws an error in jslint and my guess is that this is becasue it breaks scope rules.

The function argument ( not parameter ) is an un-named variable in the global scope and has no access to the closure.

( until is is copied into it 🙂

Would it work if I made private1 and private 2 the same object say…just private…and then accessed it using this keyword.

I would prefer to keep them separate however.

  • 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-12T18:22:39+00:00Added an answer on June 12, 2026 at 6:22 pm

    Your code has syntax errors and get seems to be better named as set. Also, your code doesn’t seem to be specific to browsers, so better to use the more generic global object than the specific window object.

    Consider something like:

    (function(global){
        var private1 = {name: 'private1'},
            private2 = {name: 'private2'},
            publik;
    
        publik = {
          set: function (object) {
            private1[object.name] = object;
          }
        };
    
        global.publik = publik;
    }(this))
    

    The publik.set method has access to private1 and private2 through a closure, you can access them through privileged methods as explained by Douglas Crockford in his article Private Members in JavaScript. Extending the above, it might be:

        publik = {
    
          set : function (...) {
            ...
          },
    
          getPrivate1: function (name) {
            return (typeof name == 'string')? private1[name] : private1;
          },
    
          ...
    
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following code to copy the content of an XML file into
I would like to have more than one button. I tried to copy code
I all, I just follow this great example: http://qt-project.org/doc/qt-4.8/tools-customcompleter.html I copy the code exactly
I copy and paste code from this URL for creating and reading/writing a proc
I've copy/pasted the code from this tutorial about sockets on Android . It builds
I'm looking for a solution where users can copy/paste PHP code into a textbox
How do you copy VBA code into a Word document and retain the VBA
I copy-pasted my js code into the google compiler and when I copied it
In eclipse when you copy a code from Internet for example and paste into
I'm having some fun with, copy/pasting code into textarea and then trying to make

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.