10 javascript writing methods

Source: Internet
Author: User
Tags mootools

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.

Related Article

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.