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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:02:06+00:00 2026-06-06T21:02:06+00:00

So I encountered a problem. I have this object called myTree . And that

  • 0

So I encountered a problem. I have this object called myTree. And that object has properties. One of the properties contains a method like this:

prep: function (variable) {
    /* some code */
}

In that method there is an array myarray and I want to know, whether it is possible to access the content of that array, and if it is, how I would do that.

I’ve made a demo on jsFiddle, and in the end of the JavaScript window you can see that I’m alerting the object prep in which myarray is contained.

http://jsfiddle.net/Wp7Xh/1/

  • 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-06T21:02:08+00:00Added an answer on June 6, 2026 at 9:02 pm

    JavaScript variables are function-scoped. It is not possible to access variables belonging to an inner scope (i.e. “function”) from an outer scope.

    If you want that kind of access, you must make the respective variable part of the outer scope.

    var myTree = function() {
      var myarray = [];
    
      this.prep = function (variable) {
        myarray.push(variable);
      };
    }
    

    In your scenario, where you have nested objects, it’s quite similar:

    var myTree = {
      myarray: [],
      prep: function (variable) {
        this.myarray.push(variable);
      }
    }
    

    The only difference is the use of the this keyword.

    When you define an object via the object literal syntax (obj = {prop: value}) instead of via a constructor (function Obj(value) { this.prop = value; }; obj = new Obj(value);), then all defined properties will be “public” by default.

    When you call a function on that object, this will point to the respective object instance.

    Accessing an “inner scope” variable from outside is still impossible. There’s no way around that.

    Generally speaking: You can access properties of the objects you construct. You can never access function local variables (except from the inside of nested functions).

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

Sidebar

Related Questions

I have encountered a problem in my application. I have two forms, one that
I have encountered this problem today and I don't have an explanation for it.
Hey this is the weirdest problem i've ever encoutered, I have 2 projects that's
I'm currently exploring powershell capabilities, but I have encountered a problem that I have
I have encountered a problem in one of my Java projects, which causes bugs.
I have been doing some JavaScript development and I encountered this problem. Consider the
Wondering if anyone else has encountered this problem when utilizing the new ability to
Has anyone else encountered the problem that jquery-1.6.2.min.js does not at all seem to
I have a class that looks like this: public class Analyst { [Column(Internal_ID)] public
I have a jsp file that contains this code, <form id=testForm> <input type=hidden value=study--study

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.