6

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/

katspaugh
  • 17,449
  • 11
  • 66
  • 103
madeye
  • 1,398
  • 3
  • 15
  • 33
  • in your code example I noticed that you wrapped functions in parentheses, like property : (function() {...}) Can you please construe for what reason that is? – d.k Jul 02 '12 at 10:02
  • @caligula http://stackoverflow.com/questions/9053842/advanced-javascript-why-is-this-function-wrapped-in-parentheses – madeye Jul 02 '12 at 10:08
  • but you don't call these functions (in your jsfiddle example), you don't use (function() {...})(), you simply warp them but do not call (function() {...}). is there any reason for that which I don't know or is this the error (at least redundant thing)? – d.k Jul 02 '12 at 10:15
  • @caligula I'm calling this object methods on other places. And it's not an error. I just want to access some variables withing these functions, and with that I have a problem. Everything else is working just fine. – madeye Jul 02 '12 at 10:23
  • Possible duplicate of [Access object properties within object](http://stackoverflow.com/questions/12789141/access-object-properties-within-object) – Damjan Pavlica Nov 10 '16 at 16:56

1 Answers1

10

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).

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • The first example contains a syntax error, looks like you meant to convert an object literal to a function but forgot `function ()` in the first line of code. – RobG Aug 30 '17 at 06:37