First, let's continue with the aboveCodeTo extend this Code:
< 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 ( "Feilinsa" , 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:
< 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 ( "Feilinsa" , 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.