Getting Started with JavaScript object-oriented programming

Source: Internet
Author: User

SOURCE Geek Network

1 functionPerson () {2     var_this = {}//Create an empty object, and then we take advantage of this "empty object" to host the properties and methods of the person3_this.sayhello =function(){4Alert ("Phello");5     }6     return_this;7 }8 9 functionteacher_1 () {Ten     var_this = person ();//Note The syntax parentheses here One     return_this; A } -  - //demonstrates how to override method syntax in a parent class the functionteacher_2 () { -     var_this =Person (); -_this.sayhello =function () { -Alert ("Thello"); +     } -     return_this; + } A //demonstrates how a subclass can invoke a method of a parent class at functionTeacher () { -     var_this =Person (); -     varSurpersay = _this.sayhello;//1. Handling through the "assignment" code idea -_this.sayhello =function () { -Surpersay.call (_this)//2. Forced invocation via the call () method -Alert ("Thello"); in     } -     return_this; to } +  - //Call us to create an object teacher the  vart =Teacher (); *T.sayhello ();//call to the SayHello we declared in person

Define your own HTML and run the code above, and feel a bit

1 //The demo uses a function to wrap it up and then executes it2 //how to deal with the case of increasing the parameters in line 5th3(function(){4     varn = "ime";5     functionPerson (name) {6         var_this = {}//Create an empty object, and then we take advantage of this "empty object" to host the properties and methods of the person7_this._name =name;8_this.sayhello =function(){9Alert ("Phello" + This. _name + ":" +n);Ten         } One         return_this; A     } -Window. person = person;//indicates that the function person is "hung" to the family variable, -}());//A pair of parentheses to the right of the curly brace, which is required to represent the self-running the //demonstrates the - functionTeacher (name) { -     var_this =Person (name); -     varSupersay =_this.sayhello; +_this.sayhello =function () { - Supersay.call (_this); +Alert ("Thello" +_this._name); A     } at     return_this; - } - vart = Teacher ("Iwen"); -T.sayhello ();

Getting Started with JavaScript object-oriented programming

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.