Interpretation of rightjs's Inheritance Mechanism 2

Source: Internet
Author: User
Tags mootools

Class. methods is a set of methods. It is a common practice to remove frequently used methods from the function body. In this way, you do not need to create them repeatedly every time you enter the function body.

From another perspective, class. methods is an automatic execution function, and Yui people seem to classify it as a module mode. There are many usage such as Ext. Many variables are bundled into a closure with methods that are only useful to the current module, reduce name conflicts.

Class. methods = (function () {// a series of method names, you can see that include involves a maximum of things var commons = $ W ('selfextended self_extended selfincluded self_extended'), extend = commons. concat ($ W ('prototype parent extend include '), include = commons. concat (['constructor']); // This is used to clear unnecessary methods in the module. // It is equivalent to the module. without (extend) or module. without (include) function clean_module (module, what) {return object. without. apply (object, [module]. concat (what = 'E '? Extend: Include) ;}; return {// method set }})();

Some of the above statements are inappropriate, such as commons. Concat (['constructor ']). You can directly commons. Concat ('constructor. But the biggest problem is the object. Without method:

   without: function() {    var filter = $A(arguments), object = filter.shift(), copy = {}, key;    for (key in object)      if (!filter.includes(key))        copy[key] = object[key];    return copy;  },

In IE, for... in loops have many attributes that cannot be traversed, such as valueof, tostring ......

Looking at the inherit method, it secretly uses the bridge mode, and uses the subclass and the parent class prototype to no longer connect together. In addition, the new prototype method is much faster than the property COPY method.

Inherit: function (parent) {// obtain the prototype property of the parent class if (parent & parent. prototype) {var s_klass = function () {}; s_klass.prototype = parent. prototype; this. prototype = new s_klass; this. parent = parent;} // collect the parent class this. ancestors = []; while (parent) {This. ancestors. push (parent); parent = parent. parent;} // corrected the constructor attribute return this on the prototype. prototype. constructor = This ;},

The following method expands static members for the new class. This is Ruby. In jquery, extend is used for both adding members to the jquery namespace and adding instances to the jquery object.

Extend: function () {$ A (arguments ). filter (ishash ). each (function (module) {// self-Scaling Mechanism Used to implement Ruby var callback = module. selfextended | module. self_extended; $ ext (this, clean_module (module, 'E'); If (callback) callback. call (module, this) ;}, this); return this ;},

Include is a new class extension prototype member, which is also the ruby naming method. In mootools, the value is implement. I think mootools and rights have learned a lot from Ruby. Ruby is indeed the strongest oo scripting language.

Include: function () {// obtain the prototype var ancestors = this for all superclasses. ancestors. map ('prototype'), ancestor; // shift parameters to a pure object array and iterate them $ A (arguments ). filter (ishash ). each (function (module) {// used for ruby-style self-contained var callback = module. selfincluded | module. self_included; // clear some dangerous methods to prevent overwriting module = clean_module (module, 'I'); For (var key in Module) {// obtain the closest ancestor = ancestors that has the same name as it. first (function (PROTO) {return isfunction (PR Oto [Key]) ;}); // If yes, override this method and return a currying this. Prototype [Key] =! Ancestor? Module [Key]: (function (name, method, super_method) {return function () {// used for method chain this. $ super = super_method; Return method. apply (this, arguments) ;};} (Key, module [Key], ancestor [Key]) ;}// execute self-contained if (callback) callback. call (module, this) ;}, this); return this ;}

In addition, class also has a method, but as an auxiliary method, it is not added to the class factory.

// Find a specific attribute class from the current class or its ancestor. findset = function (object, property) {var upcased = property. touppercase (), capcased = property. capitalize (), candidates = [object, object. constructor]. concat (object. constructor. ancestors), holder = candidates. first (function (o) {return O & (O [upcased] | o [capcased])}); Return holder? Holder [upcased] | holder [capcased]: NULL ;};

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.