请老师检查代码
来源:3-3 编程练习
听的说
2022-01-20 11:55:09
<!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(){ console.log(this.name,this.age); } static run(){ console.log('run'); } intro='this is a Person class'; show=function(){ console.log('show') } } </script> </body> </html>
1回答
同学你好,代码问题如下:
1、say方法是在实例上的,所以需要加在constructor方法中
2、run是原型上的,前面不需要加static
3、intro和show是方法上的,所以class写法也就是类上的,前面需要加static
参考:
祝学习愉快!
相似问题