-2

I want to access an object's properties from an object's function. I have tried:

var ui_conf = {
    Conf: {
        "a": "b", 
        "c": "d",
        "f": {}
    },      
    someFunc: function(paramx, paramy) {
       //access conf.a
       console.log(this.Conf) //undefiend
       console.log(window.ui_conf) //undefiend
    },
};
Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114
  • Only way `this` will reference the object is if you *directly invoke* `someFunc` from that object reference. `ui_conf.someFunc(...)`, or if you otherwise explicitly bind it. –  May 16 '18 at 12:55
  • Can you show us how you call your function? – pishpish May 16 '18 at 12:55
  • 2
    how are you calling this `someFunc` ? – Muhammad Usman May 16 '18 at 12:57
  • `ui_conf.someFunc` - from click event for example. – Itsik Mauyhas May 16 '18 at 13:00
  • 1
    Sorry ui_conf.someFunc is a callback. – Itsik Mauyhas May 16 '18 at 13:01
  • 1
    @ItsikMauyhas: There are many previous questions that address this, but if you pass a method like that, you're *only* passing the method, not the object too. So when it comes time to invoke the function, it no longer has any knowledge of the original object, so it can't set the proper `this` value. –  May 16 '18 at 13:03
  • Toward the end of [this answer](https://stackoverflow.com/a/20279485/5764553), there's a section titled "Common problem: Using object methods as callbacks/event handlers" that probably covers the problem. – Andrew Myers May 16 '18 at 13:09
  • Well @AndrewMyers you are right, I forgot to add this part to this question this is why all the downvotes.... thanks. – Itsik Mauyhas May 16 '18 at 13:11

2 Answers2

1

Most probably you are calling this function with some other reference something like

var temp = ui_conf.someFunc;

temp();

And that will give you undefined against this.Conf as this is not a reference to ui_conf.

You may want to try it like

var ui_conf = {
    Conf: {
        "a": "b",
        "c": "d",
        "f": {}
    },

    someFunc: function(paramx, paramy) {
        //access conf.a
        console.log(this.Conf) //undefiend
        console.log(window.ui_conf) //undefiend

    },
};

ui_conf.someFunc();
Muhammad Usman
  • 10,039
  • 22
  • 39
  • *"as temp as no property as Conf"* That'll confuse the OP since `this` is not a reference to the `temp` function. Maybe you meant that the `this` value in the `temp` invocation will not have that property, but it sounds like you're suggesting that `this === temp`. –  May 16 '18 at 13:05
  • @CrazyTrain just updated – Muhammad Usman May 16 '18 at 13:08
  • 1
    *"this will be the reference to temp itself"* That's what I was trying to prevent it sounding like because it's incorrect. The value of `this` is *not* a reference to the `temp` function. It will be the `window` object when the function doesn't know what else to use (or `undefined` in strict mode). –  May 16 '18 at 13:10
0

You can call your object's properties from an object function using this way because you have't call your property it only declared in you code:

var ui_conf = { 
                Conf : {
                    "a": "b", 
                    "c": "d",
                    "f": {}
                },
                someFunc: function(paramx, paramy) {
                   console.log(this.Conf) 
                },
            };
ui_conf.someFunc();
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24