Instantiating Objects from Inside the Static Method in JavaScript












1















I have the following code. For some reason when I try to instantiate the Movie objects from inside the getAllMovies static method I get the error. What am I doing wrong?



Movie.js:7 Uncaught TypeError: Cannot set property 'name' of undefined
at new Movie (Movie.js:7)

import React, { Component } from 'react';


class Movie extends Component {

constructor(name, year, genre) {
this.name = name
this.year = year
this.genre = genre
}

static getAllMovies() {

let movies =
let movie = new Movie("ss","sss","aaa")

/*
for(let index = 1; index <= 10; index++) {

let movie = new Movie(`Movie {index}`,2000 + index,`Genre {index}`)
movies.push(movie)

} */

return movies

}

}









share|improve this question


















  • 1





    Darn! I missed that. Thanks Mark. If you can post it as an answer I can accept it.

    – john doe
    Nov 25 '18 at 21:57
















1















I have the following code. For some reason when I try to instantiate the Movie objects from inside the getAllMovies static method I get the error. What am I doing wrong?



Movie.js:7 Uncaught TypeError: Cannot set property 'name' of undefined
at new Movie (Movie.js:7)

import React, { Component } from 'react';


class Movie extends Component {

constructor(name, year, genre) {
this.name = name
this.year = year
this.genre = genre
}

static getAllMovies() {

let movies =
let movie = new Movie("ss","sss","aaa")

/*
for(let index = 1; index <= 10; index++) {

let movie = new Movie(`Movie {index}`,2000 + index,`Genre {index}`)
movies.push(movie)

} */

return movies

}

}









share|improve this question


















  • 1





    Darn! I missed that. Thanks Mark. If you can post it as an answer I can accept it.

    – john doe
    Nov 25 '18 at 21:57














1












1








1








I have the following code. For some reason when I try to instantiate the Movie objects from inside the getAllMovies static method I get the error. What am I doing wrong?



Movie.js:7 Uncaught TypeError: Cannot set property 'name' of undefined
at new Movie (Movie.js:7)

import React, { Component } from 'react';


class Movie extends Component {

constructor(name, year, genre) {
this.name = name
this.year = year
this.genre = genre
}

static getAllMovies() {

let movies =
let movie = new Movie("ss","sss","aaa")

/*
for(let index = 1; index <= 10; index++) {

let movie = new Movie(`Movie {index}`,2000 + index,`Genre {index}`)
movies.push(movie)

} */

return movies

}

}









share|improve this question














I have the following code. For some reason when I try to instantiate the Movie objects from inside the getAllMovies static method I get the error. What am I doing wrong?



Movie.js:7 Uncaught TypeError: Cannot set property 'name' of undefined
at new Movie (Movie.js:7)

import React, { Component } from 'react';


class Movie extends Component {

constructor(name, year, genre) {
this.name = name
this.year = year
this.genre = genre
}

static getAllMovies() {

let movies =
let movie = new Movie("ss","sss","aaa")

/*
for(let index = 1; index <= 10; index++) {

let movie = new Movie(`Movie {index}`,2000 + index,`Genre {index}`)
movies.push(movie)

} */

return movies

}

}






javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 21:52









john doejohn doe

3,008124397




3,008124397








  • 1





    Darn! I missed that. Thanks Mark. If you can post it as an answer I can accept it.

    – john doe
    Nov 25 '18 at 21:57














  • 1





    Darn! I missed that. Thanks Mark. If you can post it as an answer I can accept it.

    – john doe
    Nov 25 '18 at 21:57








1




1





Darn! I missed that. Thanks Mark. If you can post it as an answer I can accept it.

– john doe
Nov 25 '18 at 21:57





Darn! I missed that. Thanks Mark. If you can post it as an answer I can accept it.

– john doe
Nov 25 '18 at 21:57












2 Answers
2






active

oldest

votes


















2














The Movie class needs to call super() in it's constructor to have the correct value for this:






class Component{}

class Movie extends Component {

constructor(name, year, genre) {
super()
this.name = name
this.year = year
this.genre = genre
}

static getAllMovies() {
let movie = new Movie("ss","sss","aaa")
return movie
}

}

console.log(Movie.getAllMovies())








share|improve this answer
























  • That's a really weird error message for a missing super() call...

    – Bergi
    Nov 25 '18 at 22:04











  • Agreed @Bergi, I assumed it was coming from something down the line in the build. Maybe this answer is too incomplete given that it's a react component subclass.

    – Mark Meyer
    Nov 25 '18 at 22:06











  • I have noticed that ReactJS and React Native both are really bad at providing correct/context sensitive error messages.

    – john doe
    Nov 25 '18 at 22:07



















0














I think your concept about react was wrong.
In react component, props and state are used for data management.
So the statement are below:



class Movie extends component{
constructor(props){
super(props);
this.state = {
name : this.props.name,
year : this.props.year,
genre : this.props.genre,
}
}


the above error was occurred because of your constructor






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%2f53472357%2finstantiating-objects-from-inside-the-static-method-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









    2














    The Movie class needs to call super() in it's constructor to have the correct value for this:






    class Component{}

    class Movie extends Component {

    constructor(name, year, genre) {
    super()
    this.name = name
    this.year = year
    this.genre = genre
    }

    static getAllMovies() {
    let movie = new Movie("ss","sss","aaa")
    return movie
    }

    }

    console.log(Movie.getAllMovies())








    share|improve this answer
























    • That's a really weird error message for a missing super() call...

      – Bergi
      Nov 25 '18 at 22:04











    • Agreed @Bergi, I assumed it was coming from something down the line in the build. Maybe this answer is too incomplete given that it's a react component subclass.

      – Mark Meyer
      Nov 25 '18 at 22:06











    • I have noticed that ReactJS and React Native both are really bad at providing correct/context sensitive error messages.

      – john doe
      Nov 25 '18 at 22:07
















    2














    The Movie class needs to call super() in it's constructor to have the correct value for this:






    class Component{}

    class Movie extends Component {

    constructor(name, year, genre) {
    super()
    this.name = name
    this.year = year
    this.genre = genre
    }

    static getAllMovies() {
    let movie = new Movie("ss","sss","aaa")
    return movie
    }

    }

    console.log(Movie.getAllMovies())








    share|improve this answer
























    • That's a really weird error message for a missing super() call...

      – Bergi
      Nov 25 '18 at 22:04











    • Agreed @Bergi, I assumed it was coming from something down the line in the build. Maybe this answer is too incomplete given that it's a react component subclass.

      – Mark Meyer
      Nov 25 '18 at 22:06











    • I have noticed that ReactJS and React Native both are really bad at providing correct/context sensitive error messages.

      – john doe
      Nov 25 '18 at 22:07














    2












    2








    2







    The Movie class needs to call super() in it's constructor to have the correct value for this:






    class Component{}

    class Movie extends Component {

    constructor(name, year, genre) {
    super()
    this.name = name
    this.year = year
    this.genre = genre
    }

    static getAllMovies() {
    let movie = new Movie("ss","sss","aaa")
    return movie
    }

    }

    console.log(Movie.getAllMovies())








    share|improve this answer













    The Movie class needs to call super() in it's constructor to have the correct value for this:






    class Component{}

    class Movie extends Component {

    constructor(name, year, genre) {
    super()
    this.name = name
    this.year = year
    this.genre = genre
    }

    static getAllMovies() {
    let movie = new Movie("ss","sss","aaa")
    return movie
    }

    }

    console.log(Movie.getAllMovies())








    class Component{}

    class Movie extends Component {

    constructor(name, year, genre) {
    super()
    this.name = name
    this.year = year
    this.genre = genre
    }

    static getAllMovies() {
    let movie = new Movie("ss","sss","aaa")
    return movie
    }

    }

    console.log(Movie.getAllMovies())





    class Component{}

    class Movie extends Component {

    constructor(name, year, genre) {
    super()
    this.name = name
    this.year = year
    this.genre = genre
    }

    static getAllMovies() {
    let movie = new Movie("ss","sss","aaa")
    return movie
    }

    }

    console.log(Movie.getAllMovies())






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 25 '18 at 22:00









    Mark MeyerMark Meyer

    39.3k33162




    39.3k33162













    • That's a really weird error message for a missing super() call...

      – Bergi
      Nov 25 '18 at 22:04











    • Agreed @Bergi, I assumed it was coming from something down the line in the build. Maybe this answer is too incomplete given that it's a react component subclass.

      – Mark Meyer
      Nov 25 '18 at 22:06











    • I have noticed that ReactJS and React Native both are really bad at providing correct/context sensitive error messages.

      – john doe
      Nov 25 '18 at 22:07



















    • That's a really weird error message for a missing super() call...

      – Bergi
      Nov 25 '18 at 22:04











    • Agreed @Bergi, I assumed it was coming from something down the line in the build. Maybe this answer is too incomplete given that it's a react component subclass.

      – Mark Meyer
      Nov 25 '18 at 22:06











    • I have noticed that ReactJS and React Native both are really bad at providing correct/context sensitive error messages.

      – john doe
      Nov 25 '18 at 22:07

















    That's a really weird error message for a missing super() call...

    – Bergi
    Nov 25 '18 at 22:04





    That's a really weird error message for a missing super() call...

    – Bergi
    Nov 25 '18 at 22:04













    Agreed @Bergi, I assumed it was coming from something down the line in the build. Maybe this answer is too incomplete given that it's a react component subclass.

    – Mark Meyer
    Nov 25 '18 at 22:06





    Agreed @Bergi, I assumed it was coming from something down the line in the build. Maybe this answer is too incomplete given that it's a react component subclass.

    – Mark Meyer
    Nov 25 '18 at 22:06













    I have noticed that ReactJS and React Native both are really bad at providing correct/context sensitive error messages.

    – john doe
    Nov 25 '18 at 22:07





    I have noticed that ReactJS and React Native both are really bad at providing correct/context sensitive error messages.

    – john doe
    Nov 25 '18 at 22:07













    0














    I think your concept about react was wrong.
    In react component, props and state are used for data management.
    So the statement are below:



    class Movie extends component{
    constructor(props){
    super(props);
    this.state = {
    name : this.props.name,
    year : this.props.year,
    genre : this.props.genre,
    }
    }


    the above error was occurred because of your constructor






    share|improve this answer




























      0














      I think your concept about react was wrong.
      In react component, props and state are used for data management.
      So the statement are below:



      class Movie extends component{
      constructor(props){
      super(props);
      this.state = {
      name : this.props.name,
      year : this.props.year,
      genre : this.props.genre,
      }
      }


      the above error was occurred because of your constructor






      share|improve this answer


























        0












        0








        0







        I think your concept about react was wrong.
        In react component, props and state are used for data management.
        So the statement are below:



        class Movie extends component{
        constructor(props){
        super(props);
        this.state = {
        name : this.props.name,
        year : this.props.year,
        genre : this.props.genre,
        }
        }


        the above error was occurred because of your constructor






        share|improve this answer













        I think your concept about react was wrong.
        In react component, props and state are used for data management.
        So the statement are below:



        class Movie extends component{
        constructor(props){
        super(props);
        this.state = {
        name : this.props.name,
        year : this.props.year,
        genre : this.props.genre,
        }
        }


        the above error was occurred because of your constructor







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 22:01









        JinJin

        9311323




        9311323






























            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%2f53472357%2finstantiating-objects-from-inside-the-static-method-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

            Ottavio Pratesi

            Tricia Helfer

            15 giugno