JavaScript in the prototype (Notes)

First, JavaScript each object has a prototype property, he is used to return a reference to the prototype object type. A set of basic functions we use the prototype property to provide an object class. And a new instance of an object "inherit" the operation of the given object's prototype. But this prototype in the end is how to achieve and managed it?

For instructions prototype property of the object, the JavaScript manual says: All internal JavaScript objects have read-only prototype property. Dynamically add features (attributes and methods) to its prototype, but the object can not be given a different prototype. However, user-defined objects can be assigned to a new prototype.

Let's look at an example using three classic prototype property.

  1. Add method for the scripting environment built-in objects
    code
    Array.prototype.max = function()
    {
        var i, max = this[0];
        for (i = 0; i < this.length; i++)
        {
            if (max < this[i])
                max = this[i];
        }
        return max;
    }
  2. The method of adding custom class for a user
    code
    function MyFuc(name)
    {
        this._name = name;
    }
    
    MyFuc.prototype.ShowName = function()
    {
        alert(this._name);
    }
  3. Update prototype custom class
    code
    function MyFucA()
    {
        this.MethodA = function()
        {
            alert("MyFucA.MethodA()");
        }
    }
    function MyFucB()
    {
        this.MethodB = function()
        {
            alert("MyFucB.MethodB()");
        }
    }
    
    MyFucB.prototype = new MyFucA();

The third use of inheritance (prototypal inheritance, in addition to inheritance structure is called inheritance, etc.), MyFucB inherits a method of MyFucA.

prototype there is a default attribute: constructor, is used to indicate a function to create objects (OOP says that we are constructors). The constructor property is a member of all objects have a prototype property. They include all internal JavaScript objects except Global and Math objects. constructor property as a function of the specific object instance reference configuration.

Figured out how to use the JavaScript prototype property later, here we come to study it in depth.

JavaScript is actually a derivative form we use design patterns in the prototype pattern. Let me first say something simple prototype pattern, and then look in the end in JavaScript prototype is how is it?!

What's prototype pattern?

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

Specified kind prototype objects created instance, and create new objects by copying the prototype.

Prototype model allows an object to create another object is a customizable, do not need to know any details of how to create a working principle is: by a prototype object to the creation of an object that you want to launch, to launch the objects created by requesting prototype to implement their own copy of the object is created.

Here we continued JavaScript in the prototype, why do we say it in the prototype and prototype pattern is not the same as it continues to see an example?!:

code
function RP()
{
    RP.PropertyA = 1;
    RP.MethodA = function()
    {
        alert("RP.MethodA()");
    }
    this.PropertyA = 100;
    this.MethodA = function()
    {
        alert("this.MethodA()");
    }
}

RP.prototype.PropertyA = 10;
RP.prototype.MethodA = function()
{
    alert("RP.prototype.MethodA()");
}

RP is ResearchPrototype, see examples and results analysis:

operation result:

1

RP.MethodA

100

this.MethodA

carry on

code
<script type="text/javascript">
    var rp = new RP();
    delete RP.prototype.PropertyA;
    alert(RP.prototype.PropertyA);
    delete RP.prototype.MethodA;
    RP.prototype.MethodA();
    delete rp.PropertyA;
    alert(rp.PropertyA);
    delete rp.MethodA;
    rp.MethodA();
</script>

operation result:

undefined

Object does not support this property or method

RP.prototype.MethodA();

Again

code
<script type="text/javascript">
    var rp = new RP();
    delete rp.PropertyA;
    alert(rp.PropertyA);
    delete rp.MethodA;
    rp.MethodA();
</script>

operation result:

10

RP.Prototype.MethodA()

Here RP.PropertyA and RP.MethodA just used for reference, but how to delete all the this.PropertyA and this.MethodA, but also out of the results, and the properties and methods or prototype to import it?

This is the JavaScript prototype and prototype pattern in prototype biggest difference is, this so-called prototype property in JavaScript is actually a language itself supported features, any copy did not happen here, regardless of shallow or deep. For JavaScript interpretation engine, it's dealing with. "" Or "[keyName]" when referring to the properties and methods of the object, in the first instance to find (this) object itself, if found return or execution. If you do not find, you find the prototype object (this.constructor.prototype) in the definition of whether the objects and methods are looking for, or execution if returned to find, if not found, it returns undefined (for property) or runtime error (the method).

Just because a property or method prototype imported instance of a class is dynamic lookup, so we can add properties and methods to prototype objects within the system, such as the String object is added trim methods:

code
String.prototype.trim() 
{ 
    return this.replace(/(^\s+)|(\s+$)/g, ""); 
}

Javascript is clear that this use is in the prototype prototype pattern can not explain and support.

Reproduced in: https: //www.cnblogs.com/scott_xu/archive/2008/08/31/javascript-prototype.html

Guess you like

Origin blog.csdn.net/weixin_34278190/article/details/93688630