Prototype Class Object Learning _prototype

Source: Internet
Author: User
Tags extend inheritance
Copy Code code as follows:

/* Based on Alex Arnell ' s inheritance implementation. */

var Class = (function () {
Temporarily store Parent's prototype
function subclass () {};

Ways to create a class
function Create () {
var parent = null, properties = $A (arguments);
Check whether a parent object is specified when a new class is created
If the parent class is specified, assign to parent
if (Object.isfunction (Properties[0]))
Parent = Properties.shift ();

The class that is actually used as the return, and when the instance is created, the Initialize method is invoked to initialize the
Function Klass () {
This.initialize.apply (this, arguments);
}

Add Addmethods method to Klass, after calling the Create method
You can still call the Addmethods method for class-level method extensions
Object.extend (Klass, Class.methods);
Adds two properties to the returned class, superclass: Parent class, Subclasses: Collection of subclasses
Klass.superclass = parent;
Klass.subclasses = [];

If the parent object is specified when the class is created, point the Klass prototype to an instance of the parent object and implement the prototype chain inheritance
if (parent) {
Subclass.prototype = Parent.prototype;
Klass.prototype = new Subclass;
To add a subclass to the parent class and maintain a collection of subclasses of the parent class
Parent.subclasses.push (Klass);
}

Add a method to a new class
for (var i = 0; i < properties.length; i++)
Klass.addmethods (Properties[i]);

If no initialization method is specified, an empty method is assigned to the initialization method by default
if (!klass.prototype.initialize)
Klass.prototype.initialize = prototype.emptyfunction;

/*
* Modify the constructor of the new class so that the constructor points to itself, and here specifically (if the comment falls off the line below):
* var person=class.create ();
* var p1=new person ();
* Alert (P1.constructor==person)//true
* var man=class.create (person)
* var m1=new man ();
* Alert (M1.constrcutor==man)//false
* Alert (M1.constrcutor==person)//true
* Alert (M1.construcctor==p1.constrcutor)//true
*
* See the problem? Man's constructor actually points to the constructor of person
* The root of the problem is Klass.prototype = new subclass; This sentence
* Specific reasons I do not explain, to understand the details of the JavaScript language Essence and Programming Practice 155~160 page
*/
Klass.prototype.constructor = Klass;
Return Klass;
}

Add methods to the new class when you create the class, or to add class levels after you create the class
function Addmethods (source) {
Get the parent class of the new class
var ancestor = This.superclass && this.superclass.prototype;
var properties = Object.keys (source);

Looks like the following judgment is always true, do not know why so write, know tell me?
if (! Object.keys ({tostring:true}). length) {
If the new class overrides the ToString and ValueOf methods, add the
if (source.tostring!= Object.prototype.toString)
Properties.push ("toString");
if (source.valueof!= Object.prototype.valueOf)
Properties.push ("valueof");
}

Iterate through the methods in all the new class declarations
for (var i = 0, length = properties.length i < length; i++) {
property is the function name and value is the function body
var property = Properties[i], value = Source[property];
To determine whether this method needs to invoke the same method of the parent class
if (ancestor && object.isfunction (value) &&
Value.argumentnames (). A () = "$super") {
var method = value;
It's important here!
Replace the $super argument so that this argument points to the same method as the parent class
The Wrap method of function is applied here, the explanation of wrap method refer to "Prototype Learning--function Object"
method is the new definition, so his first argument is $super and then from ' = ' to '. ' Returns a method with the same name as the parent class
Finally, the Wrap method is called to replace the $super parameter with the same method as the parent class, so that when the subclass calls $super (), the parent class's method of the same name is invoked
This is a great building! Worth thinking about
Value = (function (m) {
return function () {return ancestor[m].apply (this, arguments);};
) (property). Wrap (method);

To point the valueof and ToString of the newly generated value (that is, the modified subclass method) to the same method of the Atomic class
Here is the legacy problem after correcting the call wrap method
Value.valueof = Method.valueOf.bind (method);
Value.tostring = Method.toString.bind (method);
}
Add a method to a new class
This.prototype[property] = value;
}

return this;
}

Returns the callable method of class
return {
Create:create,
Methods: {
Addmethods:addmethods
}
};
})();

This class provides 2 methods: Create and Addmethods, the source note above has been explained clearly, the following is a few examples to specify the use of:
Copy Code code as follows:

Declare the person class and define the initialization method
var person = class.create ({
Initialize:function (name) {
THIS.name = name;
},
Say:function (message) {
return this.name + ': ' + message;
}
});

When subclassing, specify the class you want to inherit from
var Pirate = class.create (person, {
Redefine the Speak method
Notice the $super usage here, the explanation in the control source. Take a closer look.
Say:function ($super, message) {
return $super (message) + ', yarr! ';
}
});

var john = new Pirate (' Long John ');
John.say (' ahoy matey ');
-> "Long John:ahoy matey, yarr!"


Copy Code code as follows:

var john = new Pirate (' Long John ');
John.sleep ();
-> Error:sleep is isn't a method
Every person should is able to sleep, not just pirates!

Here is the use of addmethods, you can extend the method at the class level
Person.addmethods ({
Sleep:function () {
Return This.say (' ZzZ ');
}
});
John.sleep ();


Copy Code code as follows:

Here are the usages of the superclass and subclasses two properties

Person.superclass
-> NULL
Person.subclasses.length
-> 1
Person.subclasses.first () = = Pirate
-> true
Pirate.superclass = person
-> true

Three examples cover the class class, for detailed examples please refer to: http:// Prototypejs.org/learn/class-inheritance

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.