Javascript study Note 6-Proposal of Prototype

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.