When I first started to contact the prototype, I felt like the cloud was in the fog. I read some materials and found that it was not something that I could grasp overnight. I just wrote down some things and I will study it later.
(1)
Function person (name) {This. name = Name;} person. prototype. getname = function () {return this. name ;}; function user (name, password) {This. name = Name; this. password = password;}; user. prototype = new person ("aperson"); User. prototype. getPassword = function () {return this. password ;}; var OBJ = new user ("auser"," 123 "); alert (obj. getname () + "" + obj. getPassword ());Running result: "auser 123 ". OBJ inherits the getname () method defined by the person prototype.
(2)
Function person (name) {This. name = Name;} person. prototype. getname = function () {return this. name ;}; function user (name, password) {This. password = password;}; user. prototype = new person ("aperson"); User. prototype. getPassword = function () {return this. password ;}; var OBJ = new user ("auser"," 123 "); alert (obj. getname () + "" + obj. getPassword ());Running result: "aperson 123 ". The name attribute is not defined in the user function, but the new person ("aperson") is called when the OBJ object is created. OBJ obtains the name attribute of the person object by inheriting the getname () method.
If you change
User.prototype = new Person();
The undefined error is reported because the name in person is not defined.
If you replace
function User( name, password ) {
this.name = name;
this.password = password;
};
The result is "auser 123 ".
(3)
Function person (name) {This. name = Name; this. show = function () {return "ppppp" ;}} person. prototype. getname = function () {return this. name ;}; function user (name, password) {This. name = Name; this. password = password;}; user. prototype = new person ("aperson"); User. prototype. getPassword = function () {return this. password ;}; var OBJ = new user ("auser"," 123 "); alert (obj. getname () + "" + obj. getPassword () + "" + obj. show ());Running result: "auser 123 ppppp ". The show () method is not defined for the user and is inherited from the person.
After that:
Of course, this is only a description of the surface phenomenon, and the specific deep-level operating mechanism needs to be studied.