function Person(){ } Person.prototype.age=7; Person.prototype.getAge=function(){ return age; } //调用函数 var person=new Person(); alert(person.getAge());
在调用person.getAge()后,会沿着作用域链查找该getAge()方法,首先会在new Person()对象中查找,没有找到;然后进入下一个作用域链对象,此时的对象为Person.prototype,而该对象中有getAge()方法,执行该方法后,由于目前作用域链的前端对象是Person.prototype,要返回age的值,该age值的查找得从原型对象Person.prototype中进行查找,此时原型对象中的age=7;那么调用函数应该返回的age也为7,也就是说,person.getAge()返回为7,但是,最后出现了错误:
Uncaught ReferenceError: age is not defined
各位大神,为啥会报错啊??
森栏
相关分类