The prototype in JavaScript

Source: Internet
Author: User

Prototype understanding

Prototype (prototype)

In JavaScript, the prototype object is an important mechanism for implementing object-oriented.

Each function is an object ( functions), the function object has a sub-object prototype object, and the class is defined in the form of a function.

Prototype represents the prototype of the function, and also represents a collection of members of a class.

in passingWhen new creates an instance object of a class, the members of the prototype object become members of the instantiated object.

1, the object is referenced by the class, only the function object can be referenced;

2, after the new instantiation, its members are instantiated, the instance object can be called.

At the same time, the function is an object, and the function object can be called without being instantiated if it declares the member directly.

Define members:

<script language= "javascript" type= "Text/javascript" >//defines a class that has only one property prop functionClass1 () { This. prop=1; } //Use the prototype property of a function to define a new member for a classClass1.prototype.showprop=function() {alert ( This. Prop); }

Or: Use this method (Qiushi note)
Class1.prototype = {
Showprop:function () {
Alert (This.prop)
}
}
//Create an instance of Class1 varobj1=NewClass1 ();//invoke the Showprop method defined by the prototype prototype objectObj1.showprop ();</script>

JavaScript is Object-based, and any element can be considered an object. However, the types and objects are different.

In this article, in addition to discussing some of the characteristics of types and objects, it is more important to study how to write good and reusable types.

After all, JavaScript, a popular scripting language, is very meaningful for reuse if it can be well encapsulated and form a large type library.

The prototype in JavaScript

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.