Was trying to declare a function in my object but inside my code it is saying infinite loop within my code. I tried to declare it as a normal function but my IDE only wants it declared like it is below but yet it says infinite loop when I compile it.
let movie = {
title: "Forrest Gump",
director: "Robert Zemeckis",
composer: "Alan Silvestri",
budget: 55000000,
boxOffice: 677900000,
awards: [],
totalRevenue: function () {
let x = this.movie.boxOffice - this.movie.budget;
return x;
},
};
CodePudding user response:
Delete movie
in your function:
totalRevenue: function() {
let x = this.boxOffice - this.budget;
return x;
},
Your code is error because it's trying to find an object called "movie" in this
object which you don't have. Your function will work if you make your object like this:
let movie = {
title: "Forrest Gump",
director: "Robert Zemeckis",
composer: "Alan Silvestri",
budget: 55000000,
boxOffice: 677900000,
awards: [],
movie: {
boxOffice: 677900000,
budget: 55000000
},
totalRevenue: function () {
let x = this.movie.boxOffice - this.movie.budget;
return x;
},
};
CodePudding user response:
In your code this
belong to movie object, so remove movie from your function you can directly invoke boxoffice
and budget
because it is belong to the this
let movie = {
title: "Forrest Gump",
director: "Robert Zemeckis",
composer: "Alan Silvestri",
budget: 55000000,
boxOffice: 677900000,
awards: [],
totalRevenue: function () {
let x = this.boxOffice - this.budget;
return x;
},
};
movie.totalRevenue(); // will return 622900000
And one more thing your code is not going in infinite loop rather than it is unable to find movie
inside this