小弟初学JS面向对象编程 现有一问题 请教各位大虾:
Person=function (){
//私有变量定义
var name;
vae age;
var Alert=function (){ alert(name+age);};
return {
printName:function(){ alert(this.Alert());},
printAge:function(){alert(thia.age);}
}
}
外部调用 Person person1=new Person();
person1.name="张三";
person1.age=20;
person1.printAge();//成功没有错误
person1.printName();//报错
请各位指点为什么在公有方法里 用this调私有变量都可以 调私有方法都出错?
问题补充:
langshao 写道
- function WhoAmI()
- {
- alert("I‘m " + this.name + " of " + typeof(this));
- };
- WhoAmI();
- 输出:I‘m of object
- var BillGates = {name: "Bill Gates"};
- BillGates.WhoAmI = WhoAmI;
- BillGates.WhoAmI();
- var SteveJobs = {name: "Steve Jobs"};
- SteveJobs.WhoAmI = WhoAmI;
- SteveJobs.WhoAmI();
- WhoAmI.call(BillGates);
- WhoAmI.call(SteveJobs);
- 8
- BillGates.WhoAmI.call(SteveJobs);
- I‘m Steve Jobs of object
- SteveJobs.WhoAmI.call(BillGates);
- I‘m Bill Gates of object
- WhoAmI.WhoAmI = WhoAmI;
- WhoAmI.name = "WhoAmI";
- WhoAmI.WhoAmI();
- ({name: "nobody", WhoAmI: WhoAmI}).WhoAmI();
- 方法。输出:I‘m nobody of object
谢谢你 答复的很详细 太感谢了。。。你有QQ吗可以加一下吗 以后向你请教