9

I am learning about this keyword in Javascript. I am trying a way to access an outer object property with the inner object function. For example :

var outer = {
    prop : 'prop',
    func : function(){
      return this.prop;
    },
    inner : {
      func : function(){
        return this.prop;
      }
    }
  }
  --> outer.func() : 'prop'
  --> outer.inner.func() : undefined

I understand why it doesn't work but I don't know how to access the prop of the outer object.

Phi Nguyen
  • 3,046
  • 14
  • 26
  • Please see my comment to @connexo and then please elaborate on why you want to do this. – Alnitak Jun 07 '15 at 16:07
  • Because I have a function which has an argument which is an outer object without name, and there is an inner object which want to access one function of the outer . I am wondering if there is anyway to access it without calling the outer object's name. :) I see there is no way now. Thanks for your advice @Alnitak. – Phi Nguyen Jun 07 '15 at 16:29

4 Answers4

10

It's usually a very bad idea to have the insides of a function property know anything about the variable name that has been assigned to the object that contains that property. It introduces unwanted dependencies and more importantly prevents more than one instance of such an object from existing.

An alternate construct is the "module pattern" show below, using a closure and a variable that allows any nested properties to access that (local) variable.

var outer = (function() {
    var prop = 'prop';
    return {
        prop: prop,
        func: function() {
            return prop;
        },
        inner : {
            func : function() {
                return prop;
            }
        } 
    }
})();
Alnitak
  • 334,560
  • 70
  • 407
  • 495
2
var outer = {
    prop : 'prop',
    func : function(){
      return this.prop;
    },
    inner : {
      func : function(){
        return outer.prop;
      }
    }
  }
connexo
  • 53,704
  • 14
  • 91
  • 128
  • in _general_, it's a bad idea to refer to a variable by the name it has been given from a function declared as a property of that variable. It causes a dependency that shouldn't exist - normally this should be resolved using `this` references inside that function. – Alnitak Jun 07 '15 at 16:03
  • Agreed, but OP asked how to access using current construct, I assumed. – connexo Jun 07 '15 at 16:04
0

You can use a reference to outer to access props. For example:

var outer = {
    prop : 'prop',
    test : function() {
       return this === outer;
    }, 
    func : function(){
      return this.prop;
    },
    inner : {
      func : function(){
        return outer.prop;
      },
      test: function() {
        return this === outer;
    }
  }
}

console.log(outer.func())  // prop
console.log(outer.test())  // true
console.log(outer.inner.func())  // prop
console.log(outer.inner.test()) // false

https://jsfiddle.net/gk2fegte/2/

When you call this.prop within the inner object this doesn't point to outer but to the inner object. Check out the test function in the above code.

Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
0

You are using the JavaScript object literal and functions, so that the context of "this" keyword differs. You can see more details about the "this" keyword in How does "this" keyword work within a function?. And in your case, use "outer.prop" to access

Community
  • 1
  • 1
XuChu LIU
  • 238
  • 3
  • 11