Javascript prototype inheritance note

Source: Internet
Author: User
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.

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.