【问题标题】:I'm trying to add an element to an object I previously created but I get an error that property does not exist on type {} javascript我正在尝试向我之前创建的对象添加一个元素,但我收到一个错误,即 type {} javascript 上不存在属性
【发布时间】:2021-11-30 18:21:54
【问题描述】:

我刚开始学习 javascript,我正在尝试向我之前创建的对象添加一个元素,但我收到一个属性不存在的错误。

我似乎找不到问题,可能是语法错误,但我似乎看不到它,所以如果是这样的话,也许有人可以帮我发现它?

这是对象>

const restaurant = {
  name: 'Classico Italiano',
  location: 'Via Angelo Tavanti 23, Firenze, Italy',
  categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
  starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
  mainMenu: ['Pizza', 'Pasta', 'Risotto'],

  openingHours: {
    thu: {
      open: 12,
      close: 22,
    },
    fri: {
      open: 11,
      close: 23,
    },
    sat: {
      open: 0, // Open 24 hours
      close: 24,
    },
  },
  orderD: function ({
    orderName = 'ANON',
    address = 'ANON',
    time = '00:00',
    order = 'ANON',
  }) {
    console.log(`${orderName} has ordered (${order}) to ${address} at ${time}`);
  },
  orderPizza: function (mainIngredient, ...otheringredients) {
    console.log(mainIngredient);
    console.log(otheringredients);
  },
};

然后我在这里尝试添加一个元素。

restaurant.numOfGuests = 0;

但随后它会突出显示元素名称

类型'{ name: string; 上不存在属性'numOfGuests';位置:字符串;类别:字符串[];启动菜单:字符串[];主菜单:字符串[];开放时间:{周四:{开放:数字;关闭:数字; };星期五:{开放:号码;关闭:数字; };坐:{打开:号码;关闭:数字; }; }; orderD: ({ orderName, address, time, order, }: { ...; }) => void; orderPizza: (mai...'.ts(2339)

我试图忽略它并继续前进,但是当我尝试将新元素记录到控制台时

console.log(numOfGuests);

它给了我这个错误

未捕获的 ReferenceError:未定义 numOfGuests

【问题讨论】:

  • 看来您正在使用类型脚本?你的意思是?一次学习一件事可能会更好,javascript 然后 typescript。

标签: javascript arrays typescript object javascript-objects


【解决方案1】:

您似乎没有设置 numOfGuests 属性,如果您的代码是 TypeScript(而不是 JavaScript),这一点很重要。您所要做的就是按照以下代码进行设置。然后,您可以根据需要在任何其他代码中引用它。请参阅Object Types 了解更多信息。

const restaurant = {
  name: 'Classico Italiano',
  location: 'Via Angelo Tavanti 23, Firenze, Italy',
  categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
  starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
  mainMenu: ['Pizza', 'Pasta', 'Risotto'],
  numOfGuests: 20,

  openingHours: {
    thu: {
      open: 12,
      close: 22,
    },
    fri: {
      open: 11,
      close: 23,
    },
    sat: {
      open: 0, // Open 24 hours
      close: 24,
    },
  },
  orderD: function({
    orderName = 'ANON',
    address = 'ANON',
    time = '00:00',
    order = 'ANON',
  }) {
    console.log(`${orderName} has ordered (${order}) to ${address} at ${time}`);
  },
  orderPizza: function(mainIngredient, ...otheringredients) {
    console.log(mainIngredient);
    console.log(otheringredients);
  },
};
console.log(restaurant);

如果您使用的是 JavaScript,则不应出现该错误。以如下代码sn-p为例:

const restaurant = {
  name: "Classico Italiano",
  location: "Via Angelo Tavanti 23, Firenze, Italy",
  categories: ["Italian", "Pizzeria", "Vegetarian", "Organic"],
  starterMenu: ["Focaccia", "Bruschetta", "Garlic Bread", "Caprese Salad"],
  mainMenu: ["Pizza", "Pasta", "Risotto"],

  openingHours: {
    thu: {
      open: 12,
      close: 22
    },
    fri: {
      open: 11,
      close: 23
    },
    sat: {
      open: 0, // Open 24 hours
      close: 24
    }
  },
  orderD: function({
    orderName = "ANON",
    address = "ANON",
    time = "00:00",
    order = "ANON"
  }) {
    console.log(`${orderName} has ordered (${order}) to ${address} at ${time}`);
  },
  orderPizza: function(mainIngredient, ...otheringredients) {
    console.log(mainIngredient);
    console.log(otheringredients);
  }
};
restaurant.numOfGuests = 20;
console.log(restaurant);

【讨论】:

  • 我所关注的课程他们并没有那样做,他们只是稍后添加了该项目而没有将其设置在我可以做到的对象中,但我想知道发生了什么以及如何解决它
  • 该错误是因为您正在使用打字稿并且打字稿想确切地知道您的对象具有哪些字段。你可以在这里阅读更多关于它的信息typescriptlang.org/docs/handbook/typescript-from-scratch.html
  • 是的,如果您使用的是 TypeScript,则必须提供 @nlta 所述的类型。如果使用 JavaScript,则不应出现错误。我会更新我的答案来证明这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-29
  • 2019-05-23
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
相关资源
最近更新 更多