Research on Prototype attribute in JavaScript

xiaoxiao2021-04-10  354

We know that the prototype property of the object in JScript is referenced by returning to the object type prototype. We use the prototype attribute to provide a set of basic features of the class of the object. And the new instance of the object will "inherit" to confer the operation of the object prototype. But how does this prototype implement and manage it?

For the instructions for the prototype properties of the object, if you say: All JScript internal objects have a read-only Prototype property. You can dynamically add functions (properties and methods) to its prototype, but the object cannot be given different prototypes. However, user-defined objects can be assigned to a new prototype.

Let's take an example of using three classic prototype properties.

1. Add the method to add objects to the script environment:

Array.Prototype.max = function () {var i, max = this [0]; for (i = 1; i

TestObject.prototype.showname = function () {alert (this.m_name);}; 3, update custom class Prototype: function testObjecta () {this.methoda = function () {Alert ('testobjecta.methoda () }}

Function TestObjectb () {this.methodb = function () {alert ('TestObjectB.Methodb ());}}

TestObjectb.prototype = new testObjecta (); third very familiar? Yes, it is the prototype inheritance method we introduced earlier ~~ But today we are not studying "inheritance", which can be used to achieve a inheritance, just use the prototype properties of a side effect.

Prototype also has a default attribute: constructor, is used to represent a function of creating an object (ie, the constructor in our OOP). The constructor property is a member of all objects with prototype properties. They include all JScript internal objects other than Global and Math objects. The constructor property saves a reference to a function of constructing a specific object instance.

After understanding how the prototype attribute in JScript is used, let's take a deep research on it. In the above article, I rarely list all the use of Prototype properties in JScript, but prototype is not created by JScript, and JScript actually uses a derived form of Prototype Pattern in our design mode. Below I will briefly say Prototype Pattern, then see what is going on the bottom JScript ?!

What's prototype pattern?

Spectality, and create new objects by Create Using a prototypical instance, and create new objects by Copening this prototype.

Use prototype examples to designate the type of object and create a new object by copying these prototypes. The prototype mode allows an object to create another custom-made object, do not need to know any details of how to create, the working principle is: By passing a prototype object to the object to be created, this object to be created by requesting prototype Objects copy them to implement creation.

Continue to understand what prototype pattern, you can see the 'design pattern prototype (prototype)' this article, even if you don't understand Java, you don't have a relationship, put it as a C # look.

What is the prototype? Anti-point, prototype pattern is the implementation of CLONE, of course, Shallow Copy is still deep Copy's Clone to see your own needs.

Below we continue to say Prototype in JScript, why do we say that it is different from prototype pattern ?! This is not what I said, nor I blow it, look at this example, you can be confident :