10, the Mootools.js writing class way
The latest version of Mootools.js is 1.2.3, which uses 1.2.0. The Mootool is designed to be a very compact, modular, object-oriented JS Library. Class class is used to write classes in Mootool. Class classes are new from the native class:
Copy Code code 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 to 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;
}
});
Native method is a very important method in MooTools, many classes use it to assemble. such as Window,document,event. Of course, there are class, import MooTools after we write class only need to use class on the line. A person class:
Copy Code code 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;
}
})
New One object
var p = new Person ("Jack");
Test Set,get method
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 normal function, which assembles a class (function) by passing parameters, and returns that Class (function) at the end. Since native is a function, the way the function is invoked is (), call,apply. But the new Native (obj) method is used in MooTools. Why? The reason is just to make native look more like a class.