Constructor, prototype objects, inheritance

Constructor

The constructor function is used when we create a new object called. So far, we've learned a lot of built-in constructor, for example, Object, Array and Function. Objects created using the constructor of the same have the same properties and methods. In addition to these built-in constructors, we can also create your own constructor.

Constructor is also a function, and define how ordinary functions of the same. The only difference is that the first letter of the name of the constructor should be capitalized, in order to distinguish from other functions. The following defines an empty function Person.

function Person() {
    
}

Defined the constructor later, you can use it to create the objects, for example, the following two objects created using the Person constructor.

was PERSON1 = new Person ();
was person2 = new Person ();

When we use the new operator calls the function, the internal function will automatically create a new object of that type. When the function call is completed, the object is automatically returned to the external function. We can use the instanceof operator to check whether person1 and person2 is an object instance of the Person type:

console.log(person1 instanceof Person);  // true
console.log(person2 instanceof Person);  // true

Since person1 and person2 by Person constructor creates, so they return true if the instance of the Person type using instanceof operator to check.

Declare an empty constructor in fact, no use. The purpose of a constructor is to easily create many objects have the same attributes and methods. We can use the newly created object through the this keyword inside a function, as in this example:

function Person(name) {
    this.myName = name;
    this.sayName = function() {
        console.log(this.myName);
    }
}

MyName Each object has its own property, so sayName () method may return a different value depending on the object.

Prototype object

prototype property

An object instance through its internal property __proto__ track prototype object. The value of this attribute is a memory address (pointer), points to the prototype object instance (inherited) a. When we use new to create a new object, prototype object constructor will be assigned to the new object __proto__ property. The following diagram illustrates the directivity __proto__ property:

Prototype object chain

Since the prototype object is an object that has its own prototype object and inherit the prototype object attributes. This is the prototype chain of objects: object inherits its prototype object, the prototype object inherits its prototype object, and so on.

When reading the properties of an object, JavaScript engine first to find property in its own name attribute of the object. If found is returned. If you own property does not contain the property, the JavaScript will search the prototype object attributes prototype object along the chain. If found return it, otherwise return undefined.

inherit

Learn how to create objects first step is to understand object-oriented programming. The second step is to understand inheritance. In traditional object-oriented languages, classes inherit properties from other classes. However, in JS, in the case where no class inheritance between objects may occur. This inheritance mechanism that you are already familiar with, is the prototype object inherit property.

Prototype object inheritance

JS built prototype object inheritance chain is called inheritance, it can be called the prototype object inheritance. As we speak in front of it, the prototype property of the object can be accessed object instance, which is a form of inheritance. Object instance inherits the prototype object attributes.

All objects, including those that we have automatically inherited by the object from Object constructor to create a custom, unless we specify otherwise. Rather, all objects are inherited from Object.prototype. Any object created with the Object literal form, the value of its property __proto__ are automatically set to Object.prototype, which means it inherits Object.prototype properties.

Modify Object.prototype

All objects inherit properties and methods Object.prototype object. As shown in the following example, a method of adding Object.prototype:

Object.prototype.add = function (value) {
    return this + value;
};

Object.prototype added to the property on the object will be inherited by all types of objects, sometimes write makes no sense, and also may be wrong. Do not modify Object.prototype object.

Object inheritance

Object inheritance is the simplest inheritance. The only thing we need to do is specify which object is a new object prototype object. Object literal form implicitly specified Object.prototype its prototype object, we can use Object.create () method explicitly specified prototype object.

The Object.create () method takes two parameters. The first parameter is to be set to the new object's prototype object. The second parameter is selected, for attributes for new objects.

Two kinds of statements have the same effect, a first object literal statement used to define the form of a single attribute of an object having a title. The object is automatically inherited from the Object.prototype, and its properties are set to default configurable, enumerate and write. The second statement Using Object.create () method explicitly did the same operation. Exactly the same behavior two book objects. But this writing lacks significance, using literal definitions of default objects inherit Object.prototype, so we do not need to write. Inherited from other objects is even more interesting.

Guess you like

Origin www.cnblogs.com/zhengedeboke/p/12076216.html