JS Object Private Variable public variable problem

Source: Internet
Author: User

0
JS Object Private Variable public variable problem 5Little Brother Beginner JS Object-oriented Programming existing problem ask you prawns:
Person=function () {
Private variable definition
var name;
VAE age;
var alert=function () {Alert (name+age);};

return {
Printname:function () {alert (this. Alert ());},
Printage:function () {alert (thia.age);}
}
}

External call person person1=new person ();
Person1.name= "Zhang San";
person1.age=20;
Person1.printage ();//Success without errors
Person1.printname ();//Error

Would you please point out why private variables can be adjusted with this in public methods, and the private methods are all wrong?

Supplementary questions:Langshao writes JavaScript code
  1. function WhoAmI () //define a WhoAmI
  2. {
  3. Alert ("I ' m" + THIS.name + "of" + typeof (this));
  4. };
  5. WhoAmI ();  //This is the current global object of this code, which in the browser is the window object whose Name property is an empty string.
  6. Output: I ' m of object
  7. var billgates = {name: "Bill Gates"};
  8. Billgates.whoami = WhoAmI;  //WhoAmI function as a method of billgates.
  9. Billgates.whoami (); //This is the billgates. Output: I ' m Bill Gates of Object
  10. var stevejobs = {name: "Steve Jobs"};
  11. Stevejobs.whoami = WhoAmI;  //WhoAmI function as a method of stevejobs.
  12. Stevejobs.whoami (); //This is the stevejobs. Output: I ' m Steve Jobs of Object
  13. Whoami.call (billgates); //Direct billgates as this, call WhoAmI. Output: I ' m Bill Gates of Object
  14. Whoami.call (Stevejobs); //Direct stevejobs as this, call WhoAmI. Output: I ' m Steve Jobs of Object
  15. 8
  16. BillGates.WhoAmI.call (Stevejobs); //Use stevejobs as this, but call BillGates's WhoAmI method. Output:
  17. I ' m Steve Jobs of Object
  18. SteveJobs.WhoAmI.call (billgates); //Use billgates as this, but call Stevejobs's WhoAmI method. Output:
  19. I ' m Bill Gates of Object
  20. Whoami.whoami = WhoAmI;  //Set the WhoAmI function to its own method.
  21. Whoami.name = "WhoAmI";
  22. Whoami.whoami (); //This is the WhoAmI function itself. Output: I ' m WhoAmI of function
  23. ({name: "Nobody", Whoami:whoami}). WhoAmI (); //Temporarily create an anonymous object and set the property after calling WhoAmI
  24. Method. Output: I ' m Nobody of object




Thank you for your very detailed reply. Do you have QQ? Can you add it and ask for your advice later?Javascript February 06, 2010 23:12 Wanghuidong
18
001
    • Add a Comment
    • Follow (0)
2 answersSort by time by vote 0 0
Adoption of the answer to the JavaScript code
  1. function WhoAmI () //define a WhoAmI
  2. {
  3. Alert ("I ' m" + THIS.name + "of" + typeof (this));
  4. };
  5. WhoAmI ();  //This is the current global object of this code, which in the browser is the window object whose Name property is an empty string.
  6. Output: I ' m of object
  7. var billgates = {name: "Bill Gates"};
  8. Billgates.whoami = WhoAmI;  //WhoAmI function as a method of billgates.
  9. Billgates.whoami (); //This is the billgates. Output: I ' m Bill Gates of Object
  10. var stevejobs = {name: "Steve Jobs"};
  11. Stevejobs.whoami = WhoAmI;  //WhoAmI function as a method of stevejobs.
  12. Stevejobs.whoami (); //This is the stevejobs. Output: I ' m Steve Jobs of Object
  13. Whoami.call (billgates); //Direct billgates as this, call WhoAmI. Output: I ' m Bill Gates of Object
  14. Whoami.call (Stevejobs); //Direct stevejobs as this, call WhoAmI. Output: I ' m Steve Jobs of Object
  15. 8
  16. BillGates.WhoAmI.call (Stevejobs); //Use stevejobs as this, but call BillGates's WhoAmI method. Output:
  17. I ' m Steve Jobs of Object
  18. SteveJobs.WhoAmI.call (billgates); //Use billgates as this, but call Stevejobs's WhoAmI method. Output:
  19. I ' m Bill Gates of Object
  20. Whoami.whoami = WhoAmI;  //Set the WhoAmI function to its own method.
  21. Whoami.name = "WhoAmI";
  22. Whoami.whoami (); //This is the WhoAmI function itself. Output: I ' m WhoAmI of function
  23. ({name: "Nobody", Whoami:whoami}). WhoAmI (); //Temporarily create an anonymous object and set the property after calling WhoAmI
  24. Method. Output: I ' m Nobody of object
February 07, 2010 10:22 Langshao
938
000
    • Add a Comment
0 0
JavaScript code
  1. Person = function () {
  2. return {
  3. Printname: function () {this . Alert.call (this);},
  4. Printage: function () {alert (this.age);}
  5. }
  6. }
  7. function process () {
  8. //External call
  9. Person1 = new Person ();
  10. Person1.name="Zhang San";
  11. person1.age=20;
  12. Person1.  Alert = function () {alert (THIS.name + this.age);};
  13. Person1.printage ();
  14. Person1.printname ();
  15. }

JS Object Private Variable public variable problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.