无限循环的对象问题中的JavaScript函数

发布于 2025-02-05 05:36:02 字数 381 浏览 1 评论 0原文

试图在我的对象中声明功能,但是在我的代码中,它在我的代码中说了无限循环。我试图将其声明为正常函数,但我的IDE只希望它像下面一样声明,但是当我进行编译时它说无限循环。

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;
  },
};

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;
  },
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

复古式 2025-02-12 05:36:03

delete 电影在您的函数中:

totalRevenue: function() {
    let x = this.boxOffice - this.budget;
    return x;
},

您的代码是错误,因为它试图在对象中找到一个名为“电影”的对象。如果您这样做对象,您的功能将起作用:

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;
    },
};

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;
    },
};
听,心雨的声音 2025-02-12 05:36:03

在您的代码中,属于电影对象,因此从您的功能中删除电影,您可以直接调用boxoffice预算,因为它属于<代码>此

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

,还有一件事,您的代码不会在无限的循环中进行,而不是无法找到Movie Inside 此

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文