0

In a simple JS object like this:

var LeadserData = {
    agent_id: 2,

    object_queries: {
        emails: {
            url: "/manual_emails/",
            method: 'GET',
              send_data: { 
                   the_id: this.agent_id
              }
        }
    }
}

it is obviously possible to access the agent_id simply like this:

LeadserData.agent_id = 100;
alert(LeadserData.agent_id);

This obviously returns 100. But why does not the internal reference of this.agent_id work?

alert((LeadserData.object_queries.emails.send_data.the_id));

I was expecting this to come out as "100" as well, but instead it is undefined. The whole fiddle is here: https://jsfiddle.net/h88zc5nw/1/

What is the explanation for this behaviour, and how can I tweak this to return the expected 100.

ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
charliez
  • 163
  • 2
  • 14
  • `LeadserData.agent_id` is not the same as `LeadserData.object_queries.emails.send_data.the_id`. Changing one will not affect the other as they are two different properties on two different objects. – ibrahim mahrir Dec 20 '17 at 00:38
  • The syntax does not return any errors, which expect it would do if this.agent_id was not valid. Replacing it with f ex "RandomVariable" throws an error... – charliez Dec 20 '17 at 00:39
  • There's no error because there's no reason to error. The `this` value does reference an object (most likely) but just not the one you want. –  Dec 20 '17 at 00:40
  • ibrahim mahrir: So basically the reference, though referencing a dynamic variable, is static. That makes sense, but is it a way of making it dependable? The agent_id will be referenced a number of times in the code, and I would like to avoid updating every instance. – charliez Dec 20 '17 at 00:41
  • objects in javascript doesn't have scope, so in your code this refers to the Gobal scope, and for sure it undefined, checkout this, only functions ( first class objects in ja ) offer scopes so you need to wrap your code into a function .... it sounds like you're new to javascript :) [understanding-scope-in-javascript](https://scotch.io/tutorials/understanding-scope-in-javascript) – Osama Najjar Dec 20 '17 at 00:44

1 Answers1

1

First of all, you are using an object literal. You can't use this inside an object literal, at least it won't be what you think it is. It will be whatever this available while the object literal is constructed.

Second, even if we assume that the above works, numbers are native types, they won't be shared via their references. So changing LeadserData.agent_id won't affect LeadserData.object_queries.emails.send_data.the_id even if you have assigned one to the other. Native types are copied on assignment, not passed around using a reference (like you would do with an object).

ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • Thank you for the clarification. Now, as I understand ir "the_id" will then be "set" to the value of whatever it is referencing, but is there any way to make it dynamically dependend on any other value? In the above example I would love to be able to declare agent_id only in one place (as the actual code will have a number of object_queries) – charliez Dec 20 '17 at 00:47
  • @charliez Why would you add that property to the `object_queries`? They all form the same object, just `LeadserData.agent_id` is sufficient, no? – ibrahim mahrir Dec 20 '17 at 00:52
  • No problem, you're welcome ... checkout this, `(function() { var agent_id = 2, LeadserData = { object_queries: { emails: { url: "/manual_emails/", method: 'GET', send_data: { the_id: agent_id } } } }; console.log(LeadserData.object_queries.emails.send_data.the_id); })();` – Osama Najjar Dec 20 '17 at 00:57
  • Problem is that agent_id need to be used in a number of AJAX calls and I was hoping to store the calls in one object. There will be other content to each call, but agent_id will be part of many of them. I just simplified the fiddle for clarity – charliez Dec 20 '17 at 00:58