js对象私有变量公有变量问题

时间:2021-08-30 03:43:17

js对象私有变量公有变量问题5

小弟初学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 写道
  1. function WhoAmI() //定义一个函数WhoAmI
  2. {
  3. alert("I'm " + this.name + " of " + typeof(this));
  4. };
  5. WhoAmI(); //此时是this 当前这段代码的全局对象,在浏览器中就是window 对象,其name 属性为空字符串。
  6. 输出:I'm of object
  7. var BillGates = {name: "Bill Gates"};
  8. BillGates.WhoAmI = WhoAmI; //将函数WhoAmI 作为BillGates 的方法。
  9. BillGates.WhoAmI(); //此时的this 是BillGates。输出:I'm Bill Gates of object
  10. var SteveJobs = {name: "Steve Jobs"};
  11. SteveJobs.WhoAmI = WhoAmI; //将函数WhoAmI 作为SteveJobs 的方法。
  12. SteveJobs.WhoAmI(); //此时的this 是SteveJobs。输出:I'm Steve Jobs of object
  13. WhoAmI.call(BillGates); //直接将BillGates 作为this,调用WhoAmI。输出:I'm Bill Gates of object
  14. WhoAmI.call(SteveJobs); //直接将SteveJobs 作为this,调用WhoAmI。输出:I'm Steve Jobs of object
  15. 8
  16. BillGates.WhoAmI.call(SteveJobs); //将SteveJobs 作为this,却调用BillGates 的WhoAmI 方法。输出:
  17. I'm Steve Jobs of object
  18. SteveJobs.WhoAmI.call(BillGates); //将BillGates 作为this,却调用SteveJobs 的WhoAmI 方法。输出:
  19. I'm Bill Gates of object
  20. WhoAmI.WhoAmI = WhoAmI; //将WhoAmI 函数设置为自身的方法。
  21. WhoAmI.name = "WhoAmI";
  22. WhoAmI.WhoAmI(); //此时的this 是WhoAmI 函数自己。输出:I'm WhoAmI of function
  23. ({name: "nobody", WhoAmI: WhoAmI}).WhoAmI(); //临时创建一个匿名对象并设置属性后调用WhoAmI
  24. 方法。输出:I'm nobody of object

谢谢你 答复的很详细 太感谢了。。。你有QQ吗可以加一下吗 以后向你请教

2010年2月06日 23:12

js对象私有变量公有变量问题
Wanghuidong

18

0
0
1

2个答案
按时间排序
按投票排序

0
0

采纳的答案

  1. function WhoAmI() //定义一个函数WhoAmI
  2. {
  3. alert("I'm " + this.name + " of " + typeof(this));
  4. };
  5. WhoAmI(); //此时是this 当前这段代码的全局对象,在浏览器中就是window 对象,其name 属性为空字符串。
  6. 输出:I'm of object
  7. var BillGates = {name: "Bill Gates"};
  8. BillGates.WhoAmI = WhoAmI; //将函数WhoAmI 作为BillGates 的方法。
  9. BillGates.WhoAmI(); //此时的this 是BillGates。输出:I'm Bill Gates of object
  10. var SteveJobs = {name: "Steve Jobs"};
  11. SteveJobs.WhoAmI = WhoAmI; //将函数WhoAmI 作为SteveJobs 的方法。
  12. SteveJobs.WhoAmI(); //此时的this 是SteveJobs。输出:I'm Steve Jobs of object
  13. WhoAmI.call(BillGates); //直接将BillGates 作为this,调用WhoAmI。输出:I'm Bill Gates of object
  14. WhoAmI.call(SteveJobs); //直接将SteveJobs 作为this,调用WhoAmI。输出:I'm Steve Jobs of object
  15. 8
  16. BillGates.WhoAmI.call(SteveJobs); //将SteveJobs 作为this,却调用BillGates 的WhoAmI 方法。输出:
  17. I'm Steve Jobs of object
  18. SteveJobs.WhoAmI.call(BillGates); //将BillGates 作为this,却调用SteveJobs 的WhoAmI 方法。输出:
  19. I'm Bill Gates of object
  20. WhoAmI.WhoAmI = WhoAmI; //将WhoAmI 函数设置为自身的方法。
  21. WhoAmI.name = "WhoAmI";
  22. WhoAmI.WhoAmI(); //此时的this 是WhoAmI 函数自己。输出:I'm WhoAmI of function
  23. ({name: "nobody", WhoAmI: WhoAmI}).WhoAmI(); //临时创建一个匿名对象并设置属性后调用WhoAmI
  24. 方法。输出:I'm nobody of object
2010年2月07日 10:22
js对象私有变量公有变量问题
langshao

938

0
0
0
0
0
  1. Person = function() {
  2. return {
  3. printName: function() { this.Alert.call(this); },
  4. printAge: function() { alert(this.age); }
  5. }
  6. }
  7. function process() {
  8. //外部调用
  9. person1 = new Person();
  10. person1.name="张三";
  11. person1.age=20;
  12. person1.Alert = function() { alert(this.name + this.age); };
  13. person1.printAge();
  14. person1.printName();
  15. }