On the type and object _javascript skills in JavaScript

Source: Internet
Author: User

JavaScript is object-based, and any element can be viewed as an object. However, types and objects are different. In this article, in addition to discussing some of the features of types and objects, it is more important to look at how to write good and reusable types. After all, JavaScript, a popular scripting language that can be packaged well and form a large type library, makes sense for reuse.

Online for prototype a lot of articles, has not understood the core of the idea. Finally wrote a lot of examples of code to understand: prototype can only be used on the type.

Here are some examples of types and objects, and it may be easier to understand the relationship between types and objects after reading the example:

Example code
Description
1
O Bject.prototype.Property = 1;
Object.prototype.Method = function ()
    alert (1);
 
var obj = new Object ();
obj. Method (); The

JS are array, Boolean, Date, Enumerator, Error, Function, number, Object, REGEXP, String
2
V Ar obj = new Object ();
Obj.prototype.Property = 1; //error
//error
Obj.prototype.Method = function ()
    alert (1);
3
Object.property = 1;
    alert (1);
Object.Method (); The
4
Object.property = 1;
    alert (1);
var obj = new Object ();
5
function AClass ()
{
This. property = 1;
This. method = function ()
{
Alert (1);
}
}
var obj = new AClass ();
Alert (obj. property);
Obj. Method ();
This example demonstrates the usual way to define a type in JavaScript
6
function AClass ()
{
This. property = 1;
This. method = function ()
{
Alert (1);
}
}
Aclass.prototype.Property2 = 2;
ACLASS.PROTOTYPE.METHOD2 = function
{
Alert (2);
}
var obj = new AClass ();
Alert (obj. Property2);
Obj. METHOD2 ();
You can use prototype externally to add properties and methods to a custom type.
7
function AClass ()
{
This. property = 1;
This. method = function ()
{
Alert (1);
}
}
Aclass.prototype.Property = 2;
Aclass.prototype.Method = function
{
Alert (2);
}
var obj = new AClass ();
Alert (obj. property);
Obj. Method ();
The properties or methods of a custom type cannot be changed externally by prototype.
This example shows that the properties and methods invoked are still the result of the original definition.
8
function AClass ()
{
This. property = 1;
This. method = function ()
{
Alert (1);
}
}
var obj = new AClass ();
Obj. property = 2;
Obj. method = function ()
{
Alert (2);
}
Alert (obj. property);
Obj. Method ();
You can change the properties on an object. (This is for sure)
You can also change the method on the object. (unlike the common object-oriented concept)
9
function AClass ()
{
This. property = 1;
This. method = function ()
{
Alert (1);
}
}
var obj = new AClass ();
Obj. Property2 = 2;
Obj. METHOD2 = function ()
{
Alert (2);
}
Alert (obj. Property2);
Obj. METHOD2 ();
You can add a property or method to an object
10
function AClass ()
{
This. property = 1;
This. method = function ()
{
Alert (1);
}
}
function AClass2 ()
{
This. Property2 = 2;
This. METHOD2 = function ()
{
Alert (2);
}
}
Aclass2.prototype = new AClass ();
var obj = new AClass2 ();
Alert (obj. property);
Obj. Method ();
Alert (obj. Property2);
Obj. METHOD2 ();
This example shows how a type inherits from another type.
11
function AClass ()
{
This. property = 1;
This. method = function ()
{
Alert (1);
}
}
function AClass2 ()
{
This. Property2 = 2;
This. METHOD2 = function ()
{
Alert (2);
}
}
Aclass2.prototype = new AClass ();
AClass2.prototype.Property = 3;
AClass2.prototype.Method = function ()
{
Alert (4);
}
var obj = new AClass2 ();
Alert (obj. property);
Obj. Method ();
This example illustrates how subclasses can override the properties or methods of a parent class.

In the above example, the important things about reusing by type are:
• The type of behavior allowed to be added in example 1:javascript
• Examples of limitations used by 2:prototype
• Example 3: How to define a static member on a type
• Example 7:prototype restrictions on members of a redefined type
• Example 10: How to make one type inherit from another type
• Example 11: How to redefine the members of a parent class in a subclass

The object-oriented features that JavaScript can implement are:
• Public Property (field)
• Public methods (common method)
• Private Property (privacy field)
• Private method (in private field)
• Method overload (methods overload)
• Constructors (constructor)
• Events (Event)
• Single inheritance (Inherit)
• Subclasses override properties or methods of the parent class (override)
• Static properties or methods (static member)
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.