Accessing this from inside object's function without callbacks?

accessing nested objects javascript
object inside object javascript
javascript object
javascript find value in nested object
access object property in array javascript
accessing object properties with variables
javascript access object property by key
javascript object properties

I'm trying to call an object (this) (in the example it's o) from within its children.

As shown in the example: I have 10 instances of o inside wrap.instances and I want to call one of these instances from DOM call, via wrap. When I call a var t (this) is set to a() -- how can I set the scope to o instead of its function?

Thanks!

//DOM call
$('#app').on('click', '[call]', function(e) {
    wrap.call($(this).attr('call'));
});

//Wrap
const wrap = {
    instances: {},
    call: function(k) {
        this.instances[k].fn.a();
    }
}

//Instance o
let o = {
    fn: {
        a: function() {
            var t = this; //This is a, not o
            t.fn.b(); //Error, since scope is function a
        },
        b: function() { }
    }
};

try:

this.instances[k].fn.a.apply(this.instances[k]);

Object methods, "this", “this” in methods​​ It's common that an object method needs to access the information stored in the object to do its job. For instance, the code inside user. sayHi() may need the name of the user . To access the object, a method can use the this keyword. It’s common that an object method needs to access the information stored in the object to do its job. For instance, the code inside user.sayHi () may need the name of the user. To access the object, a method can use the this keyword. The value of this is the object “before dot”, the one used to call the method.

You could just .bind the function to o:

let o = {};
Object.assign(o, {
  fn: {
    a: function() {
        var t = this; // this is o
        t.fn.b(); // no error
    }.bind(o),
    b: function() { }.bind(o)
  }
});

Accessing Nested Objects in JavaScript, tldr; safely access nested objects in JavaScript in a super cool way. JavaScript is amazing, we all know that already. But a few things in  Summary: Microsoft Scripting Guy, Ed Wilson, talks about accessing objects inside other objects in the Windows PowerShell pipeline. Hey, Scripting Guy! Yesterday in Create Custom PowerShell Objects, you talked about writing a script to access embedded objects. That seems like a lot of trouble.

Because your calling context is fn, another option would be to change the a function to call this.b (so as to refer to fn.b) rather than this.fn.b (which would require a non-standard calling context of o):

let o = {
    fn: {
        a: function() {
            this.b();
        },
        b: function() { }
    }
};

Working with objects, Properties of JavaScript objects can also be accessed or set using a bracket notation (for more details see property accessors). Objects are  Access Nested Objects Using Array Reduce. Array reduce method is very powerful and it can be used to safely access nested objects. // to access nested array, just pass in array index as an element the path array. // this will return the city from the first address item.

Property accessors, Property accessors provide access to an object's properties by using the dot notation or the bracket notation. Accessing nested json objects is just like accessing nested arrays. Nested objects are the objects that are inside an another object. In the following example 'vehicles' is a object which is inside a main object called 'person'. Using dot notation the nested objects' property(car) is accessed. Example-1. Live Demo

How to: Access Members of an Object, . Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] }; Non-Static Nested Class (Inner Class) A non-static nested class is a class within another class. It has access to members of the enclosing class (outer class). It is commonly known as inner class. Since the inner class exists within the outer class, you must instantiate the outer class first, in order to instantiate the inner class.

How can I access and process nested objects, arrays or JSON , What is the name of the dot between an object and property or method? The this Keyword. In a function definition, this refers to the "owner" of the function. In the example above, this is the person object that "owns" the fullName function. In other words, this.firstName means the firstName property of this object.

Comments
  • Why not just change the call to t.b() rather than t.fn.b()?
  • I don't think it would make a difference, would it? Since I can't access the correct t in the first place
  • this is fn, not a
  • this is actually fn, not a or window
  • this.instances[k].fn.a(); will call a with a calling context of this.instances[k].fn, right? So just access this.b
  • Yeah, this works, no idea why I was trying to call .apply on function call, instead of function reference.. Thanks!
  • Just as a followup; I could not use this method due to the complexity/hierarchy of the object, perhaps the example I've given in the OP wasn't really the best