老师 批改一下作业
来源: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方法是在构造函数中声明的:

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

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

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

祝学习愉快!
相似问题