Imitate the Jquery.extend function to extend the JS code _javascript skill of the object

Source: Internet
Author: User
Tags extend instance method mul
But in the process of writing, if you want to add a new static method or an instance method to the previously written object, modify the original object structure, and then look at the jquery extend method, and indeed extend method to support the jq of half the sky, copycat, for their own objects to do expansion.

Let's get down to business:
If you have one of the following objects
Copy Code code as follows:

var MyMath = {
Addition
Add:function (A, b) {
return a + B;
},
Subtraction
Sub:function (A, b) {
return a-b;
}
}

Object name MyMath, with two static methods add and sub, normal call:

Copy Code code as follows:

Alert (Mymath.add (3, 5))//Result 8

OK, now if MyMath add two static methods (multiplication, division), and do not modify previously written objects, we can do so before:
Copy Code code as follows:

New Plus one static method: Mul multiplication
mymath["Mul"] = function (A, b) {
return a * b;
}
New Plus one static method: div Division
mymath["Div"] = function (A, b) {
return a/b;
}

So, we add two methods to MyMath: Mul and Div. Normal Call:
Copy Code code as follows:

Alert (Mymath.add (3, 5))//Result 8
Alert (Mymath.mul (3, 5))//Result 15

But the addition of the method is a bit clumsy, and each additional method is written once for the object name (MyMath), can you think of the JSON structure to declare an object when we created the object earlier?
The answer, of course, is to do it easily, by simulating the jquery.extend function. The following extracts the Jquery.extend function and modifies the function name:
Copy Code code 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 as 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 just our method (multiplication, Division):
Copy Code code as follows:

Mymath.extend ({
Mul:function (A, b) {
return a * b;
},
Div:function (A, b) {
return a/b;
}
});

Such a structure is more at a glance.
Reprint 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.