Prototype inheritance in JavaScript (i)

Source: Internet
Author: User

"Object-oriented" has three basic characteristics, namely encapsulation, inheritance and polymorphism. Generally speaking, three features are fully satisfied, we call it "object-oriented language", while the language that satisfies some features is "object-based language".

The inheritance characteristics of the "object System", there are three implementations, including class-based, prototype-based, and base-based classes. Instead of using our common class inheritance system, JavaScript uses prototype inheritance to implement the object system. So there are no classes in JavaScript, and a mechanism called "constructors" is used to implement some of the functions of a class.

On this topic I want to start with empty objects and empty objects to discuss prototype inheritance, NULL object (NULL) We need to define it first. It is a reserved word, an object type, and the object is null, and the null object can participate in the operation, but because he has no attributes and no methods, and no prototypes, So the associated call will fail. Because it is not an object () constructor instance, the instanceof () operation returns FALSE. The so-called empty object is a standard object instance constructed through object (), for example, Obj=new object (), and empty objects have all the properties of the object.

Here we discuss the basic properties of prototype inheritance, in the implementation of the JavaScript object system, the object does not have a prototype, and the constructor has a prototype, the object only "constructs from a prototype" problem, there is no "hold a prototype" problem. A prototype is actually an object instance. The meaning of the prototype is that if the constructor has a prototype object A, the instances created by the constructor are necessarily copied from a, and we have to make clear that the null object is the basis of all objects, and we use the following code to examine the most basic object () constructor:


Take prototype object

Pro =object.prototype;

Enumerate object members and Count

var num=0;

for (var n in pro) {

num++;

}

Display Count: 0

alert (num);

As can be seen, the prototype of the object () constructor is an empty object. So what's the point, which means it

Obj1=new Object ();

obj2={};

It is nothing more than copying an image of an "object" from Object.prototype, but this method is very economical when each instance is copied, and the new instance and prototype occupy the same memory space, and the other strategy is to deceive the system's technology: copy-on-write. A typical example of this kind of deception is the dynamic link library in the operating system, its memory area is always copied, this method is far away from the need to write the properties of the object, we copy a prototype image, and let the future operation point to the image.

Obviously the above rules only talk about the formation of the object, but not the construction process, in fact, the function is only a function, although he has a prototype member, but if every life a member first create an object instance, and make prototype point to it, then it is not economical, So we can actually assume that Protytope is simply not valued at the time of the function initialization, which may be the following logic

var _proto_ = null;

function Get_prototype () {

if (!_proto_) {

_proto_=new Object ();

_proto_.constructor = this;

}

return _proto_;

}

Therefore, the function has the constructor's characteristics only when it needs to be referenced to the prototype. When the prototype of a function is meaningful, it becomes a ' constructor '. This is when new is an instance, the engine constructs an object and points the object's prototype to the prototype property.

This article is from the "zdz1993" blog, make sure to keep this source http://6344239.blog.51cto.com/6334239/1580746

Prototype inheritance in JavaScript (i)

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.