10. Writing method
The latest version is 1.2.3, and 1.2.0 is used here. Mootool is designed to be a very compact, modular, object-oriented js library. Class class is used to write classes in mootool. Class class is derived from the Native class new:
/*
*Script:
*/
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 ){
if (!this[mutator]) continue;
[mutator](this, this[mutator]);
delete this[mutator];
}
= klass;
if (empty === $empty) return this;
var self = () ? (this, arguments) : this;
if ( && ) (this);
return self;
};
$extend(klass, this);
= Class;
= properties;
return klass;
}
});
The Native method is a very important method in mootools, and many classes are assembled. Such as Window, Document, Event. Of course, there is also Class here. After importing mootools, we only need to use Class when writing classes. A Person class:
/**
* Person class
* @param {Object} name
*/
var Person = new Class({
initialize: function(name){
= name;
},
setName : function(name) {
= name;
},
getName : function() {
return ;
}
})
//new an object
var p = new Person("jack");
//Test set, get method
(());//jac
('andy');
(());//andy
//Test the instanceof and whether it correctly points to Person
(p instanceof Person); //true
( == Person); //true
Native is actually just a normal function. It assembles a class (function) through the passed parameters and finally returns the class (function). Since Native is a function, the way function calls are (), call, apply. But in mootools, new Native (obj) method is used. Why? The reason is just to make Native look more like a class.
The latest version is 1.2.3, and 1.2.0 is used here. Mootool is designed to be a very compact, modular, object-oriented js library. Class class is used to write classes in mootool. Class class is derived from the Native class new:
Copy the codeThe code is as follows:
/*
*Script:
*/
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 ){
if (!this[mutator]) continue;
[mutator](this, this[mutator]);
delete this[mutator];
}
= klass;
if (empty === $empty) return this;
var self = () ? (this, arguments) : this;
if ( && ) (this);
return self;
};
$extend(klass, this);
= Class;
= properties;
return klass;
}
});
The Native method is a very important method in mootools, and many classes are assembled. Such as Window, Document, Event. Of course, there is also Class here. After importing mootools, we only need to use Class when writing classes. A Person class:
Copy the codeThe code is as follows:
/**
* Person class
* @param {Object} name
*/
var Person = new Class({
initialize: function(name){
= name;
},
setName : function(name) {
= name;
},
getName : function() {
return ;
}
})
//new an object
var p = new Person("jack");
//Test set, get method
(());//jac
('andy');
(());//andy
//Test the instanceof and whether it correctly points to Person
(p instanceof Person); //true
( == Person); //true
Native is actually just a normal function. It assembles a class (function) through the passed parameters and finally returns the class (function). Since Native is a function, the way function calls are (), call, apply. But in mootools, new Native (obj) method is used. Why? The reason is just to make Native look more like a class.