level 1
maddylin
楼主
我下面这段JS程序为什么会报错,请高人指点,报错提示如下:
demo.html:35 Uncaught TypeError: p.say is not a function
at demo.html:35
<script>
class Person{
constructor(myName, myAge){
this.name = myName;
this.age = myAge;
this.hi = function () {
console.log("hi");
}
}
run(){
console.log("run");
}
}
let obj = {
constructor: Person,
type: "人",
say: function() {
console.log(this.name, this.age);
}
};
Person.prototype = obj;
let p = new Person("lnj", 34);
p.say();
console.log(p.type);
console.log(p);
</script>
2019年11月26日 07点11分
1
demo.html:35 Uncaught TypeError: p.say is not a function
at demo.html:35
<script>
class Person{
constructor(myName, myAge){
this.name = myName;
this.age = myAge;
this.hi = function () {
console.log("hi");
}
}
run(){
console.log("run");
}
}
let obj = {
constructor: Person,
type: "人",
say: function() {
console.log(this.name, this.age);
}
};
Person.prototype = obj;
let p = new Person("lnj", 34);
p.say();
console.log(p.type);
console.log(p);
</script>