老师 批改一下作业

来源:3-3 编程练习

艳艳子

2022-07-13 18:07:22

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<script>
/*
    function Person(name, age) {
        this.name = name;
        this.age = age;
        this.say = function() {
             console.log(this.name, this.age)
        }
    }
    Person.prototype.run = function() {
        console.log("run")
    }
    Person.intro = "this is a Person class"
    Person.show = function() {
        console.log('show')
    }
*/


        class Person {
            constructor(name,age) {
                this.name = name;
                this.age = age;
            }
            say = function () {
                console.log(this.name, this.age)
            }
            static run = function () {
                console.log("run")
            }
            static intro = "this is a Person class";
            static show = function () {
                console.log('show')
            }
        }
        const zs = new Person('zs',18);
        zs.say();
</script>
</body>

</html>


写回答

1回答

好帮手慕久久

2022-07-13

同学你好,代码需要做如下调整:

1、say方法是在构造函数中声明的:

https://img.mukewang.com/climg/62ce99ee098f253005660201.jpg

那么可以把它放到constructor中声明:

https://img.mukewang.com/climg/62ce9a3009a46c0406350409.jpg

2、run不是静态方式,是实例的方法:

https://img.mukewang.com/climg/62ce9a4209f6085005440077.jpg

所以不能用static关键字定义:

https://img.mukewang.com/climg/62ce9b750942166306190606.jpg

祝学习愉快!

0

0 学习 · 17877 问题

查看课程