Closed Package
functionPerson (name) { This. Username =name; varUserage = 18; //This method allows you to impersonate a private member //similar to private members This. Setage =function(age) {Userage=Age ; } //similar to public members This. Getage =function () { returnUserage; } } varP1 =NewPerson ("Huahuah"); P1.setage (100); Alert (P1.getage ())//------------------------------------------------- varx = 100; //execution 3 found x definition functionF1 () {vary = 101; //Execute 2, find x undecided, keep lookingalert (y); alert (x); //The entire return function is often said to be a closed packet. //This function starts execution 1 and cannot find X //closures depend on the scope chain and must be released one layer at a return function () { vary = 99; alert (x); alert (y); //Look up } } functionF1 () {varFuns =NewArray (); //2: I was found, but I have cycled through the i=10 for(vari = 0; I < 10; i++) { //1: Do not find I, search for outer layer in closed packet firstFuns[i] =function() {alert (i); } } //3: Return to i=10 returnfuns; } //4: Statement myfuns=f1 () varMyfuns =F1 (); for(varn = 0; n < myfuns.length; n++) { //5: Because the length of the F1 = 10, so the length of n is also = 10, looping through the value of the popup nMyfuns[n] (); }
Prototype:
//prototype prototypes functionPerson (name, age, email) { This. UserName =name; This. Userage =Age ; This. UserEmail =email; This. sayhi=function() {alert (' Hello, my name is ' + This. UserName + ' This year ' + This. Userage + ' years old ' + ' my contact email is ' + This. UserEmail); } This. sayhellp=function() {alert ()}}//Objects created through constructors are completely independent of object objects and are not related to objects, similar to objects in C # varP1 =NewPerson ("yellow", "+", "[email protected]"); P1.sayhi (); varP2 =NewPerson ("Huang", "All", "[email protected]"); Alert (' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '); P2.sayhi (); P1. UserName= "Zhen"; P1. Userage= 15; P1. UserEmail= "[Email protected]"; P1.sayhi ();///////////////////////////prototype 2 //Defining Constructors functionPerson (name, age, email) { This. UserName =name; This. Userage =Age ; This. UserEmail =email; } //_proto_ //prototype is the prototype object for the person object //Add a Sayhi () method to the person's prototype objectPerson.prototype.sayHi =function() {alert ("My name is" + This. UserName + "," + This. Userage + "Old,my Email is" + This. UserEmail); } //creating a function object from a constructor function varP1 =NewPerson ("Susan", "[email protected]"); P1.sayhi (); varP2 =NewPerson ("Yellow", "[email protected]"); P2.sayhi ();////////////////extension methods through prototypes //adding a Haha method to a String object prototypeString.prototype.haha=function (){ return This+ "☆"; }; //creating a String Object varmsg = ' 56465455645 '; Msg=Msg.haha (); Alert (msg);///implement inheritance through prototype prototype //There is no concept of class in JS, and inheritance is implemented by objects and objects . functionPerson (name,age,email) { This. Username =name; This. Userage =Age ; This. UserEmail =email; } Person.prototype.sayHi=function() {alert ("My name is called" + This. Username + "This year" + This. Userage + "years old, my mailbox is:" + This. UserEmail); }; //Student functionStudent (SID) { This. student_id =SID; } //inheritance inherits attributes from a person through PROTOTYPE=P1Student.prototype =NewPerson ("Yellow", "[email protected]"); varS1 =NewStudent (' 1564156165 '); S1. Username= ' Lee '; Alert (S1. Username);
JavaScript Learning: The basics of using closures and prototype prototypes