Using extend and implement in Mootools to extend functions or methods for your Functions

Source: Internet
Author: User
Tags mootools

Of course, the so-called Function Extension function or method refers to the premise that the original function is not modified.
Many people know that the object-oriented mechanism of Mootools is good, but most people have misunderstood the object-oriented mechanism and think that object-oriented is a class. In fact, the object-oriented scope is very wide.

So my article is actually an object-oriented application, but this object is a function rather than a class.

// Extend new methods or attributes to a specified Function
Var Fun = function (){
Alert ('A ')
};
Fun. extend ({
Aa: function (){
Alert ('B ');
}
});
Fun ();
Fun. aa ();

In the above Code, function Fun is used to output the character "a". In this case, we understand this function as an object, if we want to add a new method for this function, so that it can output B in addition to

Therefore, I used extend to perform a method extension for the Fun object. Note that the method calling after the extension will be slightly different.

The example below demonstrates how to extend attributes and methods.

Var arr = [];
Var Fun = function (){
Arr = [1, 2];
};
Fun. extend ({
Len: function () {// extend attributes for Fun
Return arr. length;
},
Add: function (val) {// Extension Method for Fun
Arr. push (val );
}
});

Var a = Fun. len (arr );
Alert (a + '|' + arr); // output: 0 |

Fun (); // call this function to assign a value to arr

Var a = Fun. len ();
Alert (a + '|' + arr); // output at this time: 2 | 1, 2

Fun. add ('new ');

Var a = Fun. len ();
Alert (a + '|' + arr); // output at this time: 3 | 1, 2, new

Fun. add (['A', 'B', 'C']);

Var a = Fun. len ();
Alert (a + '|' + arr); // output at this time: 4 | 1, 2, new, a, B, c

Len is an extended attribute used to obtain the length of arr, while add is a method used to add a new value to the array of arr.

Implement is used to extend all function methods. In fact, it is easy to understand. We already know that there are pass, bind, and delay methods in Mootools. These methods are pre-defined by Mootools.

However, if you find that the predefined Method for us cannot meet our needs in the actual development process, what should you do now? It's very easy, as long as you just need to expand it yourself. Let's take a look at the following:

// Extend the new Function
Var a = function (){};
Var B = function (){};
Function. implement ({
Alert: function (msg) {// direct alert output content
Alert (msg );
},
Output: function (msg) {// The firebug console will output the content, and IE will report an error
Console. log (msg );
}
});
A. alert ('1 ');
A. output ('2 ');
B. output ('3 ');

After reading this article, do you have a new understanding of the object orientation of Mootools?

(Source: http://see7di.cnblogs.com)

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.