How do I access an object within itself, within another object?









up vote
0
down vote

favorite












I know that using the "this" keyword in an object references the object, but what about nested objects?






var mainObj = 
childObj:
addProperty: function()
this.message = "HELLO";


;
mainObj.childObj.addProperty();





From here, how do I access the "this.message" property, both inside and outside the "childObj" object?










share|improve this question





















  • You can call console.log(mainObj) and see where message property has been set
    – barbsan
    Nov 11 at 22:48











  • message will be at mainObj.childObj.message. Is that what you're hoping for?
    – Mark Meyer
    Nov 11 at 22:49










  • Yes, it is. Thank you so much!
    – Ghibbbeey
    Nov 11 at 22:52










  • In that case, if I wanted to add a property to mainObj replacing the code in the addProperty function would I just write mainObj.message = "HELLO" or would I write something else?
    – Ghibbbeey
    Nov 11 at 22:56














up vote
0
down vote

favorite












I know that using the "this" keyword in an object references the object, but what about nested objects?






var mainObj = 
childObj:
addProperty: function()
this.message = "HELLO";


;
mainObj.childObj.addProperty();





From here, how do I access the "this.message" property, both inside and outside the "childObj" object?










share|improve this question





















  • You can call console.log(mainObj) and see where message property has been set
    – barbsan
    Nov 11 at 22:48











  • message will be at mainObj.childObj.message. Is that what you're hoping for?
    – Mark Meyer
    Nov 11 at 22:49










  • Yes, it is. Thank you so much!
    – Ghibbbeey
    Nov 11 at 22:52










  • In that case, if I wanted to add a property to mainObj replacing the code in the addProperty function would I just write mainObj.message = "HELLO" or would I write something else?
    – Ghibbbeey
    Nov 11 at 22:56












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I know that using the "this" keyword in an object references the object, but what about nested objects?






var mainObj = 
childObj:
addProperty: function()
this.message = "HELLO";


;
mainObj.childObj.addProperty();





From here, how do I access the "this.message" property, both inside and outside the "childObj" object?










share|improve this question













I know that using the "this" keyword in an object references the object, but what about nested objects?






var mainObj = 
childObj:
addProperty: function()
this.message = "HELLO";


;
mainObj.childObj.addProperty();





From here, how do I access the "this.message" property, both inside and outside the "childObj" object?






var mainObj = 
childObj:
addProperty: function()
this.message = "HELLO";


;
mainObj.childObj.addProperty();





var mainObj = 
childObj:
addProperty: function()
this.message = "HELLO";


;
mainObj.childObj.addProperty();






javascript object nested javascript-objects






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 22:42









Ghibbbeey

4




4











  • You can call console.log(mainObj) and see where message property has been set
    – barbsan
    Nov 11 at 22:48











  • message will be at mainObj.childObj.message. Is that what you're hoping for?
    – Mark Meyer
    Nov 11 at 22:49










  • Yes, it is. Thank you so much!
    – Ghibbbeey
    Nov 11 at 22:52










  • In that case, if I wanted to add a property to mainObj replacing the code in the addProperty function would I just write mainObj.message = "HELLO" or would I write something else?
    – Ghibbbeey
    Nov 11 at 22:56
















  • You can call console.log(mainObj) and see where message property has been set
    – barbsan
    Nov 11 at 22:48











  • message will be at mainObj.childObj.message. Is that what you're hoping for?
    – Mark Meyer
    Nov 11 at 22:49










  • Yes, it is. Thank you so much!
    – Ghibbbeey
    Nov 11 at 22:52










  • In that case, if I wanted to add a property to mainObj replacing the code in the addProperty function would I just write mainObj.message = "HELLO" or would I write something else?
    – Ghibbbeey
    Nov 11 at 22:56















You can call console.log(mainObj) and see where message property has been set
– barbsan
Nov 11 at 22:48





You can call console.log(mainObj) and see where message property has been set
– barbsan
Nov 11 at 22:48













message will be at mainObj.childObj.message. Is that what you're hoping for?
– Mark Meyer
Nov 11 at 22:49




message will be at mainObj.childObj.message. Is that what you're hoping for?
– Mark Meyer
Nov 11 at 22:49












Yes, it is. Thank you so much!
– Ghibbbeey
Nov 11 at 22:52




Yes, it is. Thank you so much!
– Ghibbbeey
Nov 11 at 22:52












In that case, if I wanted to add a property to mainObj replacing the code in the addProperty function would I just write mainObj.message = "HELLO" or would I write something else?
– Ghibbbeey
Nov 11 at 22:56




In that case, if I wanted to add a property to mainObj replacing the code in the addProperty function would I just write mainObj.message = "HELLO" or would I write something else?
– Ghibbbeey
Nov 11 at 22:56












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The quick answer: within a different method of mainObj, you can use this.childObj.message, and you can use mainObj.childObj.message from the outside:






var mainObj = 
childObj:
addProperty: function()
this.message = "HELLO";

,
msg()
return this.childObj.message;
,
;
mainObj.childObj.addProperty();
console.log(mainObj.msg()); // HELLO
console.log(mainObj.childObj.message); // HELLO





Some context on this:



From the MDN documentation:




A function's this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.



In most cases, the value of this is determined by how a function is called. It can't be set by assignment during execution, and it may be different each time the function is called. ES5 introduced the bind method to set the value of a function's this regardless of how it's called, and ES2015 introduced arrow functions which don't provide their own this binding (it retains the this value of the enclosing lexical context).




Basically, depending on the context, this might mean different things:



Global context



Outside of any function, this refers to the global object. In a browser that means this === window.



Function context



In a function, the value of this depends on how you call the function, and whether you are using strict mode or not., basically it could point to the global object or inherit the execution context.



When invoking a function as a method of an object, this will be set to the object that owns the method in the invocation:






//'use strict';
window.message = 'wat';
const obj1 = message: 'hi 1', hi() return this.message; ;
const obj2 = message: 'hi 2', hi() return this.message; ;

// Normal invocation
console.log(obj1.hi())
console.log(obj2.hi())

// As functions:
const obj1Hi = obj1.hi;
console.log(obj1Hi()); // returns wat on non-strict mode!
// enable use strict to see the error thrown by this defaulting to undefined/


// Swap the functions around
obj1.hi = obj2.hi;
obj2.hi = obj1Hi;

console.log(obj1.hi()) // still uses message from obj1
console.log(obj2.hi()) // still uses message from obj2

// You can even do some `bind` magic to force the context on a function:

console.log(obj1Hi.bind(obj1)()); // hi 1
console.log(obj1Hi.bind(obj2)()); // hi 2








share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253981%2fhow-do-i-access-an-object-within-itself-within-another-object%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    The quick answer: within a different method of mainObj, you can use this.childObj.message, and you can use mainObj.childObj.message from the outside:






    var mainObj = 
    childObj:
    addProperty: function()
    this.message = "HELLO";

    ,
    msg()
    return this.childObj.message;
    ,
    ;
    mainObj.childObj.addProperty();
    console.log(mainObj.msg()); // HELLO
    console.log(mainObj.childObj.message); // HELLO





    Some context on this:



    From the MDN documentation:




    A function's this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.



    In most cases, the value of this is determined by how a function is called. It can't be set by assignment during execution, and it may be different each time the function is called. ES5 introduced the bind method to set the value of a function's this regardless of how it's called, and ES2015 introduced arrow functions which don't provide their own this binding (it retains the this value of the enclosing lexical context).




    Basically, depending on the context, this might mean different things:



    Global context



    Outside of any function, this refers to the global object. In a browser that means this === window.



    Function context



    In a function, the value of this depends on how you call the function, and whether you are using strict mode or not., basically it could point to the global object or inherit the execution context.



    When invoking a function as a method of an object, this will be set to the object that owns the method in the invocation:






    //'use strict';
    window.message = 'wat';
    const obj1 = message: 'hi 1', hi() return this.message; ;
    const obj2 = message: 'hi 2', hi() return this.message; ;

    // Normal invocation
    console.log(obj1.hi())
    console.log(obj2.hi())

    // As functions:
    const obj1Hi = obj1.hi;
    console.log(obj1Hi()); // returns wat on non-strict mode!
    // enable use strict to see the error thrown by this defaulting to undefined/


    // Swap the functions around
    obj1.hi = obj2.hi;
    obj2.hi = obj1Hi;

    console.log(obj1.hi()) // still uses message from obj1
    console.log(obj2.hi()) // still uses message from obj2

    // You can even do some `bind` magic to force the context on a function:

    console.log(obj1Hi.bind(obj1)()); // hi 1
    console.log(obj1Hi.bind(obj2)()); // hi 2








    share|improve this answer
























      up vote
      0
      down vote













      The quick answer: within a different method of mainObj, you can use this.childObj.message, and you can use mainObj.childObj.message from the outside:






      var mainObj = 
      childObj:
      addProperty: function()
      this.message = "HELLO";

      ,
      msg()
      return this.childObj.message;
      ,
      ;
      mainObj.childObj.addProperty();
      console.log(mainObj.msg()); // HELLO
      console.log(mainObj.childObj.message); // HELLO





      Some context on this:



      From the MDN documentation:




      A function's this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.



      In most cases, the value of this is determined by how a function is called. It can't be set by assignment during execution, and it may be different each time the function is called. ES5 introduced the bind method to set the value of a function's this regardless of how it's called, and ES2015 introduced arrow functions which don't provide their own this binding (it retains the this value of the enclosing lexical context).




      Basically, depending on the context, this might mean different things:



      Global context



      Outside of any function, this refers to the global object. In a browser that means this === window.



      Function context



      In a function, the value of this depends on how you call the function, and whether you are using strict mode or not., basically it could point to the global object or inherit the execution context.



      When invoking a function as a method of an object, this will be set to the object that owns the method in the invocation:






      //'use strict';
      window.message = 'wat';
      const obj1 = message: 'hi 1', hi() return this.message; ;
      const obj2 = message: 'hi 2', hi() return this.message; ;

      // Normal invocation
      console.log(obj1.hi())
      console.log(obj2.hi())

      // As functions:
      const obj1Hi = obj1.hi;
      console.log(obj1Hi()); // returns wat on non-strict mode!
      // enable use strict to see the error thrown by this defaulting to undefined/


      // Swap the functions around
      obj1.hi = obj2.hi;
      obj2.hi = obj1Hi;

      console.log(obj1.hi()) // still uses message from obj1
      console.log(obj2.hi()) // still uses message from obj2

      // You can even do some `bind` magic to force the context on a function:

      console.log(obj1Hi.bind(obj1)()); // hi 1
      console.log(obj1Hi.bind(obj2)()); // hi 2








      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        The quick answer: within a different method of mainObj, you can use this.childObj.message, and you can use mainObj.childObj.message from the outside:






        var mainObj = 
        childObj:
        addProperty: function()
        this.message = "HELLO";

        ,
        msg()
        return this.childObj.message;
        ,
        ;
        mainObj.childObj.addProperty();
        console.log(mainObj.msg()); // HELLO
        console.log(mainObj.childObj.message); // HELLO





        Some context on this:



        From the MDN documentation:




        A function's this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.



        In most cases, the value of this is determined by how a function is called. It can't be set by assignment during execution, and it may be different each time the function is called. ES5 introduced the bind method to set the value of a function's this regardless of how it's called, and ES2015 introduced arrow functions which don't provide their own this binding (it retains the this value of the enclosing lexical context).




        Basically, depending on the context, this might mean different things:



        Global context



        Outside of any function, this refers to the global object. In a browser that means this === window.



        Function context



        In a function, the value of this depends on how you call the function, and whether you are using strict mode or not., basically it could point to the global object or inherit the execution context.



        When invoking a function as a method of an object, this will be set to the object that owns the method in the invocation:






        //'use strict';
        window.message = 'wat';
        const obj1 = message: 'hi 1', hi() return this.message; ;
        const obj2 = message: 'hi 2', hi() return this.message; ;

        // Normal invocation
        console.log(obj1.hi())
        console.log(obj2.hi())

        // As functions:
        const obj1Hi = obj1.hi;
        console.log(obj1Hi()); // returns wat on non-strict mode!
        // enable use strict to see the error thrown by this defaulting to undefined/


        // Swap the functions around
        obj1.hi = obj2.hi;
        obj2.hi = obj1Hi;

        console.log(obj1.hi()) // still uses message from obj1
        console.log(obj2.hi()) // still uses message from obj2

        // You can even do some `bind` magic to force the context on a function:

        console.log(obj1Hi.bind(obj1)()); // hi 1
        console.log(obj1Hi.bind(obj2)()); // hi 2








        share|improve this answer












        The quick answer: within a different method of mainObj, you can use this.childObj.message, and you can use mainObj.childObj.message from the outside:






        var mainObj = 
        childObj:
        addProperty: function()
        this.message = "HELLO";

        ,
        msg()
        return this.childObj.message;
        ,
        ;
        mainObj.childObj.addProperty();
        console.log(mainObj.msg()); // HELLO
        console.log(mainObj.childObj.message); // HELLO





        Some context on this:



        From the MDN documentation:




        A function's this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.



        In most cases, the value of this is determined by how a function is called. It can't be set by assignment during execution, and it may be different each time the function is called. ES5 introduced the bind method to set the value of a function's this regardless of how it's called, and ES2015 introduced arrow functions which don't provide their own this binding (it retains the this value of the enclosing lexical context).




        Basically, depending on the context, this might mean different things:



        Global context



        Outside of any function, this refers to the global object. In a browser that means this === window.



        Function context



        In a function, the value of this depends on how you call the function, and whether you are using strict mode or not., basically it could point to the global object or inherit the execution context.



        When invoking a function as a method of an object, this will be set to the object that owns the method in the invocation:






        //'use strict';
        window.message = 'wat';
        const obj1 = message: 'hi 1', hi() return this.message; ;
        const obj2 = message: 'hi 2', hi() return this.message; ;

        // Normal invocation
        console.log(obj1.hi())
        console.log(obj2.hi())

        // As functions:
        const obj1Hi = obj1.hi;
        console.log(obj1Hi()); // returns wat on non-strict mode!
        // enable use strict to see the error thrown by this defaulting to undefined/


        // Swap the functions around
        obj1.hi = obj2.hi;
        obj2.hi = obj1Hi;

        console.log(obj1.hi()) // still uses message from obj1
        console.log(obj2.hi()) // still uses message from obj2

        // You can even do some `bind` magic to force the context on a function:

        console.log(obj1Hi.bind(obj1)()); // hi 1
        console.log(obj1Hi.bind(obj2)()); // hi 2








        var mainObj = 
        childObj:
        addProperty: function()
        this.message = "HELLO";

        ,
        msg()
        return this.childObj.message;
        ,
        ;
        mainObj.childObj.addProperty();
        console.log(mainObj.msg()); // HELLO
        console.log(mainObj.childObj.message); // HELLO





        var mainObj = 
        childObj:
        addProperty: function()
        this.message = "HELLO";

        ,
        msg()
        return this.childObj.message;
        ,
        ;
        mainObj.childObj.addProperty();
        console.log(mainObj.msg()); // HELLO
        console.log(mainObj.childObj.message); // HELLO





        //'use strict';
        window.message = 'wat';
        const obj1 = message: 'hi 1', hi() return this.message; ;
        const obj2 = message: 'hi 2', hi() return this.message; ;

        // Normal invocation
        console.log(obj1.hi())
        console.log(obj2.hi())

        // As functions:
        const obj1Hi = obj1.hi;
        console.log(obj1Hi()); // returns wat on non-strict mode!
        // enable use strict to see the error thrown by this defaulting to undefined/


        // Swap the functions around
        obj1.hi = obj2.hi;
        obj2.hi = obj1Hi;

        console.log(obj1.hi()) // still uses message from obj1
        console.log(obj2.hi()) // still uses message from obj2

        // You can even do some `bind` magic to force the context on a function:

        console.log(obj1Hi.bind(obj1)()); // hi 1
        console.log(obj1Hi.bind(obj2)()); // hi 2





        //'use strict';
        window.message = 'wat';
        const obj1 = message: 'hi 1', hi() return this.message; ;
        const obj2 = message: 'hi 2', hi() return this.message; ;

        // Normal invocation
        console.log(obj1.hi())
        console.log(obj2.hi())

        // As functions:
        const obj1Hi = obj1.hi;
        console.log(obj1Hi()); // returns wat on non-strict mode!
        // enable use strict to see the error thrown by this defaulting to undefined/


        // Swap the functions around
        obj1.hi = obj2.hi;
        obj2.hi = obj1Hi;

        console.log(obj1.hi()) // still uses message from obj1
        console.log(obj2.hi()) // still uses message from obj2

        // You can even do some `bind` magic to force the context on a function:

        console.log(obj1Hi.bind(obj1)()); // hi 1
        console.log(obj1Hi.bind(obj2)()); // hi 2






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 23:08









        lucascaro

        3,38611530




        3,38611530



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253981%2fhow-do-i-access-an-object-within-itself-within-another-object%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto