Summary of object creation and prototype in Javascript

Source: Internet
Author: User

Basic knowledge
1. Strictly speaking, in JS, only objects without class
2. Objects can have unique attributes and methods. That is, the attributes and methods of an object are supersets of classes.
3. ecmascript can recognize two types of objects.
A type of object called Native object belongs to the linguistic category;
A host object is provided by the runtime environment, such as the Document Object and Dom node.
Native object is a loose structure that allows you to dynamically add, delete, and modify attributes and functions of an object ). The attribute has one name and one value. The attribute value can be referenced by another object.

Or the built-in data type (string, number, Boolean, null, or undefined ). Methods include methods and methods.
You can directly specify the property value (method body) for the Property (method). If this property (method) does not exist in the object, this property (method) is created ).
You can call Delete obj. A to delete a property or method.

1. About prototype
1. The prototype attribute corresponds to an object at the class level. Access through classname. prototype.
2. When creating a class object, these objects share the prototype attribute of the class.
3. The prototype attribute points to an object. The corresponding class of this object may also have the prototype attribute. Therefore, prototype chains may be formed.
4. When defining a class, its prototype has a default value.

 
5.
The read operation reads the first attribute value with the same name found on the object itself and prototype chain.

The write operation creates an attribute with the same name for the object (if the attribute name does not exist ).
6. classa. Prototype = A; classa. Prototype = B;


Then B will replace a with the prototype of classa.
7.
Objects pointed to by the prototype attribute, which can contain attributes and methods. Methods for assigning values to the prototype attribute are as follows:


 
Classa. Prototype = obja; // specify the object directly



 
Classa. Prototype =
{A: 1, B: function (){}};
Classa. Prototype. A = 1; // set
The prototype property points to the property of the object.
Classa. Prototype. B = function (){};
//
Set
The prototype property points to the B method of the object.

(Http://www.javaeye.com/topic/53537
2. Create an object



Class definition has three basic methods:


1. Factory functions. For example



Function cofactory (){
VaR o = new object;
O. A = 1 return o
}
Create an object in this way: var o = cofactory ();

2. constructor. For example



Function Co (){





This. A = 1
}



Create an object in this way: var o = new CO ().



3. constructor + prototype. For example



Function Co (){



}


CO. Prototype. A = 1;
Create an object in this way: var o = new CO ();


Class objects have static attributes and methods. Can be accessed through classa..

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.