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