Passing data values between two classes in JavaScript












0















I guess it is quite regular issue, but for some reason I cannot find the answer on the net.



So I have a class A:



class A {
const data = {...}
}


And a class B in a separate js file.



class B {
// how can I get const data here?
}









share|improve this question


















  • 2





    Even if someone answers you, I don't think this is your real code for which you cannot guarantee the answer will work. So the code does not make sense which you have provided.

    – Ankit Agarwal
    Nov 22 '18 at 9:14











  • @AnkitAgarwal - don't complicate things by trying to judge code snippets on the merit of how 'real' they are. Just take it as a hypothetical, doesn't need to be a real-world scenario as long as it is specific enough. Not specific? Then request clarity.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:15











  • @Barth - in which context? Web? If so, depending on what it is you want to share, a variable on the page will be globally accessible to all. Whether this makes sense design-wise depends on the requirements.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:15











  • @JᴀʏMᴇᴇ then why are you asking the above question to OP if you think I am complicating the things.

    – Ankit Agarwal
    Nov 22 '18 at 9:17











  • @AnkitAgarwal - what are you talking about? We were both making 2 entirely different points!? I'm asking for clarification.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:25
















0















I guess it is quite regular issue, but for some reason I cannot find the answer on the net.



So I have a class A:



class A {
const data = {...}
}


And a class B in a separate js file.



class B {
// how can I get const data here?
}









share|improve this question


















  • 2





    Even if someone answers you, I don't think this is your real code for which you cannot guarantee the answer will work. So the code does not make sense which you have provided.

    – Ankit Agarwal
    Nov 22 '18 at 9:14











  • @AnkitAgarwal - don't complicate things by trying to judge code snippets on the merit of how 'real' they are. Just take it as a hypothetical, doesn't need to be a real-world scenario as long as it is specific enough. Not specific? Then request clarity.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:15











  • @Barth - in which context? Web? If so, depending on what it is you want to share, a variable on the page will be globally accessible to all. Whether this makes sense design-wise depends on the requirements.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:15











  • @JᴀʏMᴇᴇ then why are you asking the above question to OP if you think I am complicating the things.

    – Ankit Agarwal
    Nov 22 '18 at 9:17











  • @AnkitAgarwal - what are you talking about? We were both making 2 entirely different points!? I'm asking for clarification.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:25














0












0








0








I guess it is quite regular issue, but for some reason I cannot find the answer on the net.



So I have a class A:



class A {
const data = {...}
}


And a class B in a separate js file.



class B {
// how can I get const data here?
}









share|improve this question














I guess it is quite regular issue, but for some reason I cannot find the answer on the net.



So I have a class A:



class A {
const data = {...}
}


And a class B in a separate js file.



class B {
// how can I get const data here?
}






javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 9:12









MurakamiMurakami

420213




420213








  • 2





    Even if someone answers you, I don't think this is your real code for which you cannot guarantee the answer will work. So the code does not make sense which you have provided.

    – Ankit Agarwal
    Nov 22 '18 at 9:14











  • @AnkitAgarwal - don't complicate things by trying to judge code snippets on the merit of how 'real' they are. Just take it as a hypothetical, doesn't need to be a real-world scenario as long as it is specific enough. Not specific? Then request clarity.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:15











  • @Barth - in which context? Web? If so, depending on what it is you want to share, a variable on the page will be globally accessible to all. Whether this makes sense design-wise depends on the requirements.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:15











  • @JᴀʏMᴇᴇ then why are you asking the above question to OP if you think I am complicating the things.

    – Ankit Agarwal
    Nov 22 '18 at 9:17











  • @AnkitAgarwal - what are you talking about? We were both making 2 entirely different points!? I'm asking for clarification.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:25














  • 2





    Even if someone answers you, I don't think this is your real code for which you cannot guarantee the answer will work. So the code does not make sense which you have provided.

    – Ankit Agarwal
    Nov 22 '18 at 9:14











  • @AnkitAgarwal - don't complicate things by trying to judge code snippets on the merit of how 'real' they are. Just take it as a hypothetical, doesn't need to be a real-world scenario as long as it is specific enough. Not specific? Then request clarity.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:15











  • @Barth - in which context? Web? If so, depending on what it is you want to share, a variable on the page will be globally accessible to all. Whether this makes sense design-wise depends on the requirements.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:15











  • @JᴀʏMᴇᴇ then why are you asking the above question to OP if you think I am complicating the things.

    – Ankit Agarwal
    Nov 22 '18 at 9:17











  • @AnkitAgarwal - what are you talking about? We were both making 2 entirely different points!? I'm asking for clarification.

    – JᴀʏMᴇᴇ
    Nov 22 '18 at 9:25








2




2





Even if someone answers you, I don't think this is your real code for which you cannot guarantee the answer will work. So the code does not make sense which you have provided.

– Ankit Agarwal
Nov 22 '18 at 9:14





Even if someone answers you, I don't think this is your real code for which you cannot guarantee the answer will work. So the code does not make sense which you have provided.

– Ankit Agarwal
Nov 22 '18 at 9:14













@AnkitAgarwal - don't complicate things by trying to judge code snippets on the merit of how 'real' they are. Just take it as a hypothetical, doesn't need to be a real-world scenario as long as it is specific enough. Not specific? Then request clarity.

– JᴀʏMᴇᴇ
Nov 22 '18 at 9:15





@AnkitAgarwal - don't complicate things by trying to judge code snippets on the merit of how 'real' they are. Just take it as a hypothetical, doesn't need to be a real-world scenario as long as it is specific enough. Not specific? Then request clarity.

– JᴀʏMᴇᴇ
Nov 22 '18 at 9:15













@Barth - in which context? Web? If so, depending on what it is you want to share, a variable on the page will be globally accessible to all. Whether this makes sense design-wise depends on the requirements.

– JᴀʏMᴇᴇ
Nov 22 '18 at 9:15





@Barth - in which context? Web? If so, depending on what it is you want to share, a variable on the page will be globally accessible to all. Whether this makes sense design-wise depends on the requirements.

– JᴀʏMᴇᴇ
Nov 22 '18 at 9:15













@JᴀʏMᴇᴇ then why are you asking the above question to OP if you think I am complicating the things.

– Ankit Agarwal
Nov 22 '18 at 9:17





@JᴀʏMᴇᴇ then why are you asking the above question to OP if you think I am complicating the things.

– Ankit Agarwal
Nov 22 '18 at 9:17













@AnkitAgarwal - what are you talking about? We were both making 2 entirely different points!? I'm asking for clarification.

– JᴀʏMᴇᴇ
Nov 22 '18 at 9:25





@AnkitAgarwal - what are you talking about? We were both making 2 entirely different points!? I'm asking for clarification.

– JᴀʏMᴇᴇ
Nov 22 '18 at 9:25












2 Answers
2






active

oldest

votes


















3














You could use composition, and instantiate new instance of class A inside of constructor of class B.



class A {
constructor() {
this.data = { foo: "bar" };
}
}

class B {
constructor() {
this.instanceOfA = new A();
console.log(this.instanceOfA.data);
}
}

console.log(new B());





share|improve this answer
























  • Thank you for your answer. What if in class A my variable is located in the body(I mean not in the constructor)?

    – Murakami
    Nov 22 '18 at 9:36













  • I don't think that's valid syntax of ES6. Or do you use some other version?

    – Borys Kupar
    Nov 22 '18 at 9:38



















1














Here is data passed by function/event in two class:



   class A {
constructor(name) {
console.log(name)
}
}

class B {
y(){
return "ram"
}
}

let resultFromB = (new B().y());
new A(resultFromB)





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',
    autoActivateHeartbeat: false,
    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%2f53427374%2fpassing-data-values-between-two-classes-in-javascript%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You could use composition, and instantiate new instance of class A inside of constructor of class B.



    class A {
    constructor() {
    this.data = { foo: "bar" };
    }
    }

    class B {
    constructor() {
    this.instanceOfA = new A();
    console.log(this.instanceOfA.data);
    }
    }

    console.log(new B());





    share|improve this answer
























    • Thank you for your answer. What if in class A my variable is located in the body(I mean not in the constructor)?

      – Murakami
      Nov 22 '18 at 9:36













    • I don't think that's valid syntax of ES6. Or do you use some other version?

      – Borys Kupar
      Nov 22 '18 at 9:38
















    3














    You could use composition, and instantiate new instance of class A inside of constructor of class B.



    class A {
    constructor() {
    this.data = { foo: "bar" };
    }
    }

    class B {
    constructor() {
    this.instanceOfA = new A();
    console.log(this.instanceOfA.data);
    }
    }

    console.log(new B());





    share|improve this answer
























    • Thank you for your answer. What if in class A my variable is located in the body(I mean not in the constructor)?

      – Murakami
      Nov 22 '18 at 9:36













    • I don't think that's valid syntax of ES6. Or do you use some other version?

      – Borys Kupar
      Nov 22 '18 at 9:38














    3












    3








    3







    You could use composition, and instantiate new instance of class A inside of constructor of class B.



    class A {
    constructor() {
    this.data = { foo: "bar" };
    }
    }

    class B {
    constructor() {
    this.instanceOfA = new A();
    console.log(this.instanceOfA.data);
    }
    }

    console.log(new B());





    share|improve this answer













    You could use composition, and instantiate new instance of class A inside of constructor of class B.



    class A {
    constructor() {
    this.data = { foo: "bar" };
    }
    }

    class B {
    constructor() {
    this.instanceOfA = new A();
    console.log(this.instanceOfA.data);
    }
    }

    console.log(new B());






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 9:30









    Borys KuparBorys Kupar

    757414




    757414













    • Thank you for your answer. What if in class A my variable is located in the body(I mean not in the constructor)?

      – Murakami
      Nov 22 '18 at 9:36













    • I don't think that's valid syntax of ES6. Or do you use some other version?

      – Borys Kupar
      Nov 22 '18 at 9:38



















    • Thank you for your answer. What if in class A my variable is located in the body(I mean not in the constructor)?

      – Murakami
      Nov 22 '18 at 9:36













    • I don't think that's valid syntax of ES6. Or do you use some other version?

      – Borys Kupar
      Nov 22 '18 at 9:38

















    Thank you for your answer. What if in class A my variable is located in the body(I mean not in the constructor)?

    – Murakami
    Nov 22 '18 at 9:36







    Thank you for your answer. What if in class A my variable is located in the body(I mean not in the constructor)?

    – Murakami
    Nov 22 '18 at 9:36















    I don't think that's valid syntax of ES6. Or do you use some other version?

    – Borys Kupar
    Nov 22 '18 at 9:38





    I don't think that's valid syntax of ES6. Or do you use some other version?

    – Borys Kupar
    Nov 22 '18 at 9:38













    1














    Here is data passed by function/event in two class:



       class A {
    constructor(name) {
    console.log(name)
    }
    }

    class B {
    y(){
    return "ram"
    }
    }

    let resultFromB = (new B().y());
    new A(resultFromB)





    share|improve this answer




























      1














      Here is data passed by function/event in two class:



         class A {
      constructor(name) {
      console.log(name)
      }
      }

      class B {
      y(){
      return "ram"
      }
      }

      let resultFromB = (new B().y());
      new A(resultFromB)





      share|improve this answer


























        1












        1








        1







        Here is data passed by function/event in two class:



           class A {
        constructor(name) {
        console.log(name)
        }
        }

        class B {
        y(){
        return "ram"
        }
        }

        let resultFromB = (new B().y());
        new A(resultFromB)





        share|improve this answer













        Here is data passed by function/event in two class:



           class A {
        constructor(name) {
        console.log(name)
        }
        }

        class B {
        y(){
        return "ram"
        }
        }

        let resultFromB = (new B().y());
        new A(resultFromB)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 11:29









        Anupam MauryaAnupam Maurya

        968




        968






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427374%2fpassing-data-values-between-two-classes-in-javascript%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







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga