Use Javascript objects

Source: Internet
Author: User

Using this and prototype, You can implement Object-Oriented Programming of JavaScript and build the concepts of classes and instances. First, the class is the abstraction of the object, and the instance is the actual object. The object can be instantiated with the keyword "new.

 

This is used to access the instance from the program code of the instance. The prototype object is used to set the features and methods of the class level, rather than those of the instance. The method can be stored inside the class to prevent the instance from repeatedly copying program code.

 

The following is a simple implementation:

 

<Html>

<Head>

<Script>

Function Person (id, name ){

This. id = id;

This. name = name;

}

Person. prototype. showme = function (){

Return "id =" + this. id + "; name =" + this. name;

}

Function test (){

Var p = new Person (1, "jim ");

Var p2 = new Person (2, "kate ");

Alert (p. showme () + "\ n" + p2.showme ());

}

</Script>

</Head>

<Body>

<Input type = "button" value = "test" onclick = "test ()"/>

</Body>

</Html>

 

 

 

This article is from the "jian Jian" blog

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.