Foreshadowing: JS is a prototype-based language, which is different from traditional Java-based inheritance, in addition to inheriting the methods of the class itself (actually copying from the parent class), the implementation of JS inheritance also inherits the prototype (from the prototype chain ). The JS framework encapsulates this inheritance in different depth and shows how inheritance is implemented in these four frameworks. To implement such a scenario, a window class, the window class derives a dialog. The dialog and window have the same constructor and different type names. They both contain the GetType and getconstructor methods.
Native:
// Window VaR Window = Function(){ This . Init. Apply ( This , Arguments) ;}; window. Prototype = {init: Function(Name){ This . Type = This . Getconstructor (); This . Name = Name}, GetType: Function(){ Return This . Type ;}, getconstructor: Function(){ Return 'Window ';}}; // Dialog VaR Dialog = Function(){ Window. Apply ( This , Arguments ); // Inherit the constructor }; Dialog. Prototype = New Window ();// Inherit the prototype Dialog. Prototype. getconstructor = Function(){ // Extend the parent class Method Return 'Dialog ';}; VaR _ Window = New Window ('box1 ′); VaR _ Dialog = New Dialog ('box2 '); console. Log (_ window. GetType = _ dialog. GetType ); // True
This is the implementation of native inheritance. We can see that the inheritance process consists of three parts: inherited constructor, inherited prototype, and extended.
Jquery:
// Window VaR Window =Function(){ This . Init. Apply ( This , Arguments) ;}; window. Prototype = {init: Function(Name){ This . Type = This . Getconstructor (); This . Name = Name}, GetType: Function(){ Return This . Type ;}, getconstructor:Function(){ Return 'Window ';}}; // Dialog VaR Dialog = Function(){ Window. Apply ( This , Arguments) ;}; jquery. Extend (dialog. prototype, window. prototype, {getconstructor: Function(){ Return 'Dialog ';}}); VaR _ Window = New Window ('box1 ′); VaR _ Dialog = New Dialog ('box2 '); console. Log (_ window. GetType = _ dialog. GetType );
Jquery's extend is relatively simple, but simple expansion is not a real inheritance, so implementationCodeRelatively redundant.
Prototype:
// Window VaR Window = Class. Create ({initialize: Function(){ This . Type = This . Getconstructor (); This . Name = Name ;}, GetType: Function(){ Return This . Type ;}, getconstructor: Function(){ Return 'Window ';}}); // Dialog VaR Dialog = Class. Create (window, {getconstructor: Function(){ Return 'Dialog ';}}); VaR _ Window = New Window ('box1 ′); VaR _ Dialog = New Dialog ('box2 '); console. Log (_ window. GetType = _ dialog. GetType );// True
Prototype inheritance is concise and semantic, but this class is encapsulated in prototype mode. initialize (constructor) must be specified when defining the class ), other methods are mounted to prototype by default. However, to add the class method, you need to use class. Pro = something, and this attribute cannot be directly inherited.
Mootools:
// Window VaR Window = New Class ({initialize: Function(Name){ This . Type = This . Getconstructor (); This . Name = Name ;}, GetType: Function(){ Return This . Type ;}, getconstructor: Function(){ Return 'Window ';}}); // Dialog VaR Dialog = New Class ({extends: window, getconstructor: Function(){ Return 'Dialog ';}}); VaR _ Window = New Window ('box1 ′); VaR _ Dialog = New Dialog ('box2 '); console. Log (_ window. GetType = _ dialog. GetType );
The implementation idea of mootools is similar to that of prototype. It has the advantage of concise semantics and lacks flexibility. The reason is the same as that of prototype.
Yui3
// Window VaR Window = Function(){ This . Init. Apply ( This , Arguments) ;}; window. Prototype = {init: Function(Name){ This . Type = This . Getconstructor (); This . Name = Name}, GetType:Function(){ Return This . Type ;}, getconstructor: Function(){ Return 'Window ';}}; // Dialog VaR Dialog = Function(){ This . Constructor. superclass. constructor. Apply ( This , Arguments ); This . Init. Apply ( This , Arguments) ;}; Y. Extend (dialog, window, {getconstructor: Function(){ Return 'Dialog ';}}); VaR _ Window = New Window ('box1 ′); VaR _ Dialog = New Dialog ('box2 '); console. Log (_ window. GetType = _ dialog. GetType );
In fact, Y. extend is composed of Y. mix configuration, so, Y. extend can be inherited from the prototype chain or from the methods and attributes of the class itself. I personally feel that it is a flexible implementation, but when writing code, you need to write this by hand. constructor. superclass. constructor. apply (this, arguments) to inherit the constructor, so the code is not concise and intuitive.
Other frameworks such as ext and dojo are basically prototype revisions, but the definition of Class is changed from class. Create to declare.
In general, compare the advantages and disadvantages of these four JS frameworks in terms of inheritance:
- Jquery, no flexibility, code redundancy, and weakest
- Prototype, concise and semantic code, but not flexible, moderate
- Mootools: the code is concise and semantic, but has no flexibility and is medium.
- Yui, code is bloated, but with high flexibility and moderate
In short: the implementation or powerful functions of concise semantics are always a balance.