Extends the JS Code of your object by imitating the jquery. Extend function.

Source: Internet
Author: User
Tags mul

However, during the writing process, we found that if we want to add a new static method or instance method to the previously written object, we need to modify the original object structure, so we checked the extend method of jquery, sure enough, the extend method supports the half-day of JQ and uses it as an extension for its own objects.

Enter the subject below:
Assume that there is a next objectCopyCodeCode: var mymath = {
// Addition
Add: function (a, B ){
Return A + B;
},
// Subtraction
Sub: function (a, B ){
Return A-B;
}
}

The object name is mymath. There are two static methods: Add and sub, which can be called normally:

Copy code Code: Alert (mymath. Add (3, 5) // result 8

Now, what if two static methods (multiplication and division) are added to mymath and do not modify the previously written objects? We can do this before:Copy codeThe Code is as follows: // Add a new static method: Mul Multiplication
Mymath ["Mul"] = function (a, B ){
Return a * B;
}
// Add a new static method: div Division
Mymath ["Div"] = function (a, B ){
Return A/B;
}

In this way, we add two methods to mymath: Mul and Div. Normal call:Copy codeCode: Alert (mymath. Add (3, 5) // result 8
Alert (mymath. Mul (3, 5) // result 15

However, the method added just now is a bit clumsy. Every time you add a method, you need to write an object name (mymath). Can you think about the object we created before, what if I declare an object through the JSON structure?
The answer is yes, of course. By simulating the jquery. Extend function, you can easily do it. The following extracts the jquery. Extend function and modifies the function name: Copy code The Code is as follows: mymath. Extend = function (){
// Copy reference to target object
VaR target = arguments [0] |
{}, I = 1, length = arguments. length, deep = false, options;
// Handle a deep copy situation
If (typeof target = "Boolean "){
Deep = target;
Target = arguments [1] |
{};
// Skip the Boolean and the target
I = 2;
}
// Handle case when target is a string or something (possible in deep copy)
If (typeof target! = "Object "&&! Jquery. isfunction (target ))
Target = {};
// Extend jquery itself if only one argument is passed
If (length = I ){
Target = this;
-- I;
}
For (; I <length; I ++)
// Only deal with non-null/undefined values
If (Options = arguments [I])! = NULL)
// Extend the base object
For (VAR name in options ){
VaR src = target [name], copy = options [name];
// Prevent never-ending loop
If (target = copy)
Continue;
// Recurse if we're merging object values
If (Deep & Copy & typeof copy = "object "&&! Copy. nodetype)
Target [name] = jquery. Extend (deep, // never move original objects, clone them
SRC | (copy. length! = NULL? []: {}), Copy );
// Don't bring in undefined values
Else
If (copy! = Undefined)
Target [name] = copy;
}
// Return the modified object
Return target;
};

Now we use this extend method to add our method (multiplication and division ):Copy codeThe Code is as follows: mymath. Extend ({
Mul: function (a, B ){
Return a * B;
},
Div: function (a, B ){
Return A/B;
}
});

This structure is more clear at a glance.
Reprinted please note from: http://www.cnblogs.com/wbkt2t

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.