JavaScript Write class way ten _js object oriented

Source: Internet
Author: User
Tags mootools
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.
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.