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