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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:32:50+00:00 2026-05-31T03:32:50+00:00

This is a question about scope in javascript and making APIs, I have the

  • 0

This is a question about scope in javascript and making APIs, I have the following code:

function test_objectExplorer()
{
    var obj = {
        a:true,
        b:12,
        c:{
            a:12,
            b:null
        },
        d:function(msg){
            alert(msg);
        }
    };
    alert(objectExplorer(obj));
}


function objectExplorer(obj)
{

var explore = function(obj,depthCount)
{
    var str = "";
    for(var prop in obj)
    {
        for(i=0; i<depthCount; i++)str += "    ";
        str += prop + " = " + obj[prop] + "\n";
        if( (typeof obj[prop] == 'string') || obj[prop] instanceof String)continue;
        str += explore(obj[prop],depthCount+1);
    }
    return str;
}
return explore(obj,0);
}

I use the objectExplorer function for ‘looking’ at javascript objects. It is the internal explore function which actually does the work, as you see, but I used the 2nd argument as a depth counter to provide appropriate text indentation for the output string.

I didn’t want users to have to use a function which required an additional argument which always has to be supplied in the first instance as the number zero, so I wrapped it in the ‘API’ function call objectExplorer. My question is, is this the right approach to take? I come from C#, Java and other class-based OOP languages and the API in those are just the public properties and methods but in javascript I know of no way to make things private other than hiding their declaration inside another object.

  • 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-05-31T03:32:52+00:00Added an answer on May 31, 2026 at 3:32 am

    In JavaScript the function determines the scope of variables. So

    function() {
      for (var i=0 ; i<10;i++) {
        var a = i; 
      }
      alert(a);
    }
    

    will alert 9, because the variable is within the same function.

    If you want to make modules maybe this will help you out: http://blog.davidpadbury.com/2011/08/21/javascript-modules/

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

Sidebar

Related Questions

I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
This question is about a similar javascript scope problem that I asked yesterday .
This question about Timers for windows services got me thinking: Say I have (and
I have seen this question about deploying to WebSphere using the WAS ant tasks.
I have a question about this question . I posted a reply there but
This is a basic question about what options are available for defining the scope
I have a question about best practices for Ruby variable-scope using class_eval . In
My question is really more about scope in JavaScript, rather then closures. Let's take
This question is a follow-up on a question about Python variable scope . Additional
I have a question about variable scope and memory management in C. I am

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.