无味33 2022-04-05 22:57 采纳率: 63.3%
浏览 22
已结题

如何将teacher类中的grade方法的值转化为大写呀

将teacher类中的grade方法的值转化为大写,并在读取grade是没有信息,输出nograde


```html
class Teacher {
        constructor(options) {
          this.name = options.name;
          this.age = options.age;
          this.course = options.course;
          this.score = options.score;
          this.grade = options.grade;
        }
      }
      const teacher = new Teacher({
        name: "tom",
        age: "30",
        course: "computer safe",
        score: "99",
        grade: "grade one",
      });
      console.log(teacher);

```

  • 写回答

1条回答 默认 最新

  • 林一怂儿 新星创作者: 前端开发技术领域 2022-04-06 09:07
    关注

    es2020版本以上可以这样写,可以看下是不是符合你想要的

    img

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
        <script type="module">
            class Teacher {
                #grade = "";
                constructor(options) {
                    this.name = options.name;
                    this.age = options.age;
                    this.course = options.course;
                    this.score = options.score;
                    this.#grade = options.grade;
                }
    
                get grade() {
                    if (this.#grade) return this.#grade.toLocaleUpperCase();
                    return "nograde"
                }
            }
            const teacher1 = new Teacher({
                name: "tom",
                age: "30",
                course: "computer safe",
                score: "99",
                grade: "grade one",
            });
            const teacher2 = new Teacher({
                name: "tom",
                age: "30",
                course: "computer safe",
                score: "99"
            });
            console.log(teacher1.grade);
            console.log(teacher2.grade);
        </script>
    </body>
    
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月14日
  • 已采纳回答 4月6日
  • 创建了问题 4月5日