when JavaScript creates each function, FN, there is a Ptototype property, and this prototype is actually a pointer , and this pointer always points to a
Prototype object, the purpose of this prototype object is to include a particular property or method within itself, from which all instances of a shared function are implemented.
What's that? You don't get it, all right! Let me explain briefly to you, I have to ask you to follow my thoughts first . Or would you not know that I
What's being said.
Everything is the object, lustful like a wolf I naturally take the girl as an object is a function in JavaScript, white is I put the girl this metaphor
into a function of javasript. And I do. It is her boyfriend, (haha, joking, I like this person does not have a girlfriend), temporarily cast off the concept of dust
and to simplify. I'll make love. This behavior is defined as a function. The code is as follows:
Defines an empty function (girl) functions Mm (name,age) {this.name=name;this.age=age;this. sex=sexwithboy;//calling external Sex This method};//defines an empty function (boy) functions Gg (name,age) {this.name=name;this.age=age;this. sex=sexwithboy;//Normal invocation of external sexual behavior (in reality, this is also possible.) )};//sex function Sexwithboy () {console.info (' You can have a sex with me ');};/ /instanced Object var mm=new mm (' Pertty ', 22); Example out of a name called ' Pertty ', the age of 22-year-old girl out//through the instance object mm can call her properties and method behavior, such as Console.info (Mm.name); Perttyconsole.info (mm.age);//22mm. Sex ();//You can have the sex with me.//instantiation object var gg=new gg (' Handsome ', 23);//instance a name called ' Handsome ', the age of 23-year-old boy out/ Through the instance object mm can invoke his properties and method behavior, such as Console.info (Gg.name); Handsomeconsole.info (gg.age);//23mm. Sex ();//You can have the sex with me.
Did you see it? The instance of the boy can also successfully executed the Sexwithboy this method, is not feel good terrible ah! Boys and Men
The child can also have sex, although, the reality is can. However, it is very difficult for the girls. At first I was at least numb to this phenomenon and felt with
no longer relevant. Even to say, I am happy. After all, I lost two opponents on the way to my girlfriend . But it really makes me face the girls.
Two sad eyes, I unkind. I had to coax them to be happy. So, I taught them to do this, and it would be possible to have sex with a boy. This behavior binds only to girls
In the prototype of this function, this way, the Sexwithboy method can be called only among the sisters. And the boys won't have to call Sexwithboy again.
Permissions. The code can implement this requirement based on the above minor modifications, as follows:
Defines an empty function (girl) function mm (name,age) {this.name=name;this.age=age;// this. Sex=sexwithboy ();//Invoke external sexual Behavior this method};// on the girl's prototype, customizing the private properties and behavior mm.prototype={// Remember, it's important to remember. If you forget to write here, the girl's heart will run to the national husband to go (note:// If you write this part less, the code runs, its scope will always point to object, not itself.) // constructor:mm,constructor:mm, // points to the current itself// so writing can bind Sexwithboy this method to the girl's prototype, Sexwithboy:function () {console.info (' you can have sex with me. ');}};/ / defines an empty function (boy) Function gg (name,age) {this.name=name;this.age=age;//this. sexwithboy=sexwithboy;// this call to external sex This method will have an error that does not define this method (// reality, which is also possible.) )};// Sex// function sexwithboy () {// console.info (' you can have sex With me. '); / };// instantiation of the Girl Pertty object var mm=new mm (' Pertty ', a); // instance out of a name called ' Pertty ', 22-year-old girl out// through the instance object mm can invoke her properties and method behavior, such as Console.info (Mm.name); // perttyconsole.info (Mm.age) ;// 22mm. Sexwithboy ();// you can have sex with me.// instantiation of the Girl Pertty2 object var mm2=new mm (' Pertty '); // instance out of a name called ' Pertty2 ', the age of 22-year-old girl out// through the instance object mm can invoke her properties and method behavior, such as Console.info (Mm2.name); // Pertty2console.info (mm2.age);// 20mm2. Sexwithboy ();// you can have sex with me.// instantiates the object Var gg=new gg (' Handsome ',;// instance out of a name called ' Handsome ', a boy aged 23 years out// through an instance object mm can invoke his properties and method behavior, such as Console.info (Gg.name); handsomeconsole.info (gg.age);// 23gg. Sexwithboy ();// typeerror: gg. Sexwithboy is not a function
Did you see it? This time will sexwithboy this method binds to the girl (MM) This prototype, only the girl this object can share with the boy sex happy, while the boy wants to call this method, immediately throws Gg. Sexwithboy is a function of this error. Well, by binding a method to a prototype object of an intrinsic object, you can implement the sharing of all instances. For other external object instances, it is not available!
This is a prototype explanation of my JavaScript prototype. Do you understand me? I am a head of the erotic people, I think this explanation can let me accept the understanding. Therefore, in order to better digest it, I have to take the erotic things to help me better understand this prototype. Lust is not my fault, the wrong thing is that I look at things with a pornographic eye. As long as the right place to go up, it will have its value. All right, no more talking nonsense. Hope, you can understand! The scope of the prototype is OK!
So simple on the original basis of the modification, I will again see the girls face smile is so lovely. I thought the girls would fall in love with me! However, I found that I was wrong, they still choose others, my heart is so tired! However, I believe, will always find a girl who likes me so lustful wolf! I've been looking forward to her coming!
PS: The code actually has a place to optimize, but mainly for the sake of simplicity. I don't have any more detail. About JavaScript inheritance and interfaces, design patterns, and so on, I will be late in the future to share personal insights.
Talk about my view of prototype in JavaScript.