First, let's continue with the above Code. Let's extend this Code:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var Person = function (name, age ){
This. name = name;
This. age = age;
This. Introduce = function (){
Alert ("My name is" + this. name + ". I 'm" + this. age );
};
};
Var person1 = new Person ("feilinsha", 21 );
Var person2 = new Person ("kym", 26 );
Alert (person1.Introduce = person2.Introduce );
</Script>
The result is false. That is to say, the methods of these two objects are different. We know that in C #, each object maintains a method table, but the method table should point to the same address. If so, when we declare 100 objects, do we need to create 100 object copies? Is it a waste of space?
So we thought about this solution, using prototype:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var Person = function (name, age ){
This. name = name;
This. age = age;
};
Person. prototype. Introduce = function (){
Alert ("My name is" + this. name + ". I 'm" + this. age );
}
Var person1 = new Person ("feilinsha", 21 );
Var person2 = new Person ("kym", 26 );
Alert (person1.Introduce = person2.Introduce );
</Script>
In this way, you can. So will you say whether prototype is the same? In fact, I used to understand this too. I saw this problem in this accidental test.