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
- function WhoAmI () //define a WhoAmI
- {
- Alert ("I ' m" + THIS.name + "of" + typeof (this));
- };
- 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.
- Output: I ' m of object
- var billgates = {name: "Bill Gates"};
- Billgates.whoami = WhoAmI; //WhoAmI function as a method of billgates.
- Billgates.whoami (); //This is the billgates. Output: I ' m Bill Gates of Object
- var stevejobs = {name: "Steve Jobs"};
- Stevejobs.whoami = WhoAmI; //WhoAmI function as a method of stevejobs.
- Stevejobs.whoami (); //This is the stevejobs. Output: I ' m Steve Jobs of Object
- Whoami.call (billgates); //Direct billgates as this, call WhoAmI. Output: I ' m Bill Gates of Object
- Whoami.call (Stevejobs); //Direct stevejobs as this, call WhoAmI. Output: I ' m Steve Jobs of Object
- 8
- BillGates.WhoAmI.call (Stevejobs); //Use stevejobs as this, but call BillGates's WhoAmI method. Output:
- I ' m Steve Jobs of Object
- SteveJobs.WhoAmI.call (billgates); //Use billgates as this, but call Stevejobs's WhoAmI method. Output:
- I ' m Bill Gates of Object
- Whoami.whoami = WhoAmI; //Set the WhoAmI function to its own method.
- Whoami.name = "WhoAmI";
- Whoami.whoami (); //This is the WhoAmI function itself. Output: I ' m WhoAmI of function
- ({name: "Nobody", Whoami:whoami}). WhoAmI (); //Temporarily create an anonymous object and set the property after calling WhoAmI
- 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
2 answersSort by time by vote 0 0
-
-
-
Adoption of the answer to the JavaScript code
- function WhoAmI () //define a WhoAmI
- {
- Alert ("I ' m" + THIS.name + "of" + typeof (this));
- };
- 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.
- Output: I ' m of object
- var billgates = {name: "Bill Gates"};
- Billgates.whoami = WhoAmI; //WhoAmI function as a method of billgates.
- Billgates.whoami (); //This is the billgates. Output: I ' m Bill Gates of Object
- var stevejobs = {name: "Steve Jobs"};
- Stevejobs.whoami = WhoAmI; //WhoAmI function as a method of stevejobs.
- Stevejobs.whoami (); //This is the stevejobs. Output: I ' m Steve Jobs of Object
- Whoami.call (billgates); //Direct billgates as this, call WhoAmI. Output: I ' m Bill Gates of Object
- Whoami.call (Stevejobs); //Direct stevejobs as this, call WhoAmI. Output: I ' m Steve Jobs of Object
- 8
- BillGates.WhoAmI.call (Stevejobs); //Use stevejobs as this, but call BillGates's WhoAmI method. Output:
- I ' m Steve Jobs of Object
- SteveJobs.WhoAmI.call (billgates); //Use billgates as this, but call Stevejobs's WhoAmI method. Output:
- I ' m Bill Gates of Object
- Whoami.whoami = WhoAmI; //Set the WhoAmI function to its own method.
- Whoami.name = "WhoAmI";
- Whoami.whoami (); //This is the WhoAmI function itself. Output: I ' m WhoAmI of function
- ({name: "Nobody", Whoami:whoami}). WhoAmI (); //Temporarily create an anonymous object and set the property after calling WhoAmI
- Method. Output: I ' m Nobody of object
February 07, 2010 10:22 Langshao
938
000
0 0
-
-
-
JavaScript code
- Person = function () {
- return {
- Printname: function () {this . Alert.call (this);},
- Printage: function () {alert (this.age);}
- }
- }
- function process () {
- //External call
- Person1 = new Person ();
- Person1.name="Zhang San";
- person1.age=20;
- Person1. Alert = function () {alert (THIS.name + this.age);};
- Person1.printage ();
- Person1.printname ();
- }
JS Object Private Variable public variable problem