简体   繁体   English

JS setter 返回 ''undefined'' 并且 getter 导致 Uncaught TypeError TypeError: Cannot convert undefined or null to object

[英]JS setters return ''undefined'' and the getter causes Uncaught TypeError TypeError: Cannot convert undefined or null to object

I am trying to create a simple js program of "Today's menu" using setters and getters.我正在尝试使用 setter 和 getter 创建一个简单的“今日菜单”js 程序。 The program is intended to return a random key and a value of the food and the price respectivly from an array of objects.该程序旨在从一组对象中分别返回一个随机键和一个食物值和价格。

The problem is that even it the logic is somehow OK the setters return undefined when i call:问题是,即使它的逻辑在某种程度上是确定的,当我调用时设置器返回未定义:

console.log(menu.priceToCheck);
console.log(menu.mealToCheck) ;

Because of this matter when i finally try to call the getter it causes the error:由于这个问题,当我最终尝试调用 getter 时,它会导致错误:

TypeError TypeError: Cannot convert undefined or null to object TypeError TypeError:无法将未定义或 null 转换为 object

The expected output should be for example:预期的 output 应该是例如:

The menu today has pizza for only 13 dollars今天的菜单有披萨,只要 13 美元

There is something i cannot understand as a newbee with javascript.作为 javascript 的新手,有些东西我无法理解。 Here is my code below:下面是我的代码:

let menu = {
  
    _meals : [
        {"burger": 14},
        {"mouzakas": 12},
        {"pasta": 10},
        {"pizza": 13},
    ],

    
    set meal(mealToCheck) {
      if (typeof mealToCheck === 'string') {
        let random_index = Math.floor(Math.random() * this._meals.length);
        let new_obj = this._meals[random_index];
        return new_obj = mealToCheck;
        
      }
      else {
        return 'input error';
      }
      
    },
    
    set price(priceToCheck) {
      if (typeof priceToCheck === 'number') {
        
        return Object.values(this.new_obj) = priceToCheck;
      }
      else {
        return 'input error';
      }
     
    },
    
    get todaysSpecial() {
     if (Object.keys(this.new_obj) !== '' && Object.values(this.new_obj) !== 0) {
        return  'The menu today has ' + Object.keys(this.new_obj) + ' for only ' + Object.values(this.new_obj) + ' dollars';

      }
     else {
       return 'Meal or price was not set correctly!'
      }
   }
    
    
    
    
    
    };
    
    console.log(menu.priceToCheck);
    console.log(menu.mealToCheck) ;
    console.log(menu.todaysSpecial);

You are never using the setters for priceToCheck or mealToCheck and you never defined a getter for that您永远不会使用priceToCheckmealToCheck的设置器,并且您从未为此定义过 getter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法将未定义或null转换为对象 - Uncaught TypeError: Cannot convert undefined or null to object 未捕获的类型错误:无法将未定义或 null 转换为 object React JS - Uncaught TypeError: Cannot convert undefined or null to object React JS 如何解决Uncaught TypeError:无法将undefined或null转换为对象 - how to solve Uncaught TypeError: Cannot convert undefined or null to object 如何解决这个“Uncaught TypeError:无法将undefined或null转换为object” - How to resolve this “ Uncaught TypeError: Cannot convert undefined or null to object ” json数据错误未捕获的TypeError:无法将未定义或null转换为对象 - json data error Uncaught TypeError: Cannot convert undefined or null to object 未捕获的类型错误:无法将未定义或 null 转换为 object 反应 - Uncaught TypeError: Cannot convert undefined or null to object React 未捕获的类型错误:无法在推送时将 undefined 或 null 转换为对象(<anonymous> ) - Uncaught TypeError: Cannot convert undefined or null to object at push (<anonymous>) JavaScript 未捕获类型错误:无法将未定义或 null 转换为 object - JavaScript Uncaught TypeError: Cannot convert undefined or null to object 未捕获的类型错误:无法将未定义或 null 转换为对象 - Uncaught TypeError: Cannot convert undefined or null to objec d3.v5.min.js:2 未捕获类型错误:无法将未定义或 null 转换为 object - d3.v5.min.js:2 Uncaught TypeError: Cannot convert undefined or null to object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM