10. Writing Methods of mootools. js
The latest version of mootools. js is 1.2.3, Which is 1.2.0. Mootool is designed as a very compact, modular, and object-oriented js library. The Class is used for writing classes in mootool. The Class is new from the Native Class:
Copy codeThe Code is as follows:
/*
* Script: Class. js
*/
Var Class = new Native ({
Name: 'class ',
Initialize: function (properties ){
Properties = properties | {};
Var klass = function (empty ){
For (var key in this) this [key] = $ unlink (this [key]);
For (var mutator in Class. Mutators ){
If (! This [mutator]) continue;
Class. Mutators [mutator] (this, this [mutator]);
Delete this [mutator];
}
This. constructor = klass;
If (empty ===$ empty) return this;
Var self = (this. initialize )? This. initialize. apply (this, arguments): this;
If (this. options & this. options. initialize) this. options. initialize. call (this );
Return self;
};
$ Extend (klass, this );
Klass. constructor = Class;
Klass. prototype = properties;
Return klass;
}
});
The Native method is a very important method in mootools. Many classes use it for assembly. Such as Window, Document, Event. Of course, there is also the Class here. After importing mootools, we only need to use the Class to write the Class. One Person class:
Copy codeThe Code is as follows:
/**
* Person class
* @ Param {Object} name
*/
Var Person = new Class ({
Initialize: function (name ){
This. name = name;
},
SetName: function (name ){
This. name = name;
},
GetName: function (){
Return this. name;
}
})
// A new object
Var p = new Person ("jack ");
// Test the set and get methods.
Console. log (p. getName (); // jac
P. setName ('andy ');
Console. log (p. getName (); // andy
// Test whether instanceof and p. constructor correctly point to Person
Console. log (p instanceof Person); // true
Console. log (p. constructor = Person); // true
Native is actually just a common function. It assembles a class (function) through the passed parameters, and finally returns the class (function ). Since Native is a function, the function call method is (), call, and apply. However, the new Native (obj) method is used in mootools. Why? The reason is that Native looks more like a class.