Instantiating Objects from Inside the Static Method in JavaScript
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
add a comment |
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
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
add a comment |
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
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
javascript reactjs
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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())
That's a really weird error message for a missingsuper()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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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())
That's a really weird error message for a missingsuper()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
add a comment |
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())
That's a really weird error message for a missingsuper()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
add a comment |
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())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())answered Nov 25 '18 at 22:00
Mark MeyerMark Meyer
39.3k33162
39.3k33162
That's a really weird error message for a missingsuper()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
add a comment |
That's a really weird error message for a missingsuper()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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 25 '18 at 22:01
JinJin
9311323
9311323
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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