作者:手机用户2502899537 | 来源:互联网 | 2023-10-11 00:09
123456789function Person(name){ this.name=name;}Person.prototype.getName=function(){return this.na
1 2 3 4 5 6 7 8 9
| function Person(name){
this.name=name;
}
Person.prototype.getName=function(){
return this.name;
}
//调用函数
var person=new Person("Nicholas");
alert(person.getName()); |
疑问:
同一个函数,为啥在去掉this(原型模式中的this)后代码如下:
1 2 3
| Person.prototype.getName=function(){
return name;
} |
出现如下的错误:
1
| Test3.html:102 Uncaught ReferenceError: age is not defined |
this不去掉的话,就会输出Nicholas;