JQuery.fn.extend provides an extension mechanism that allows us to extend an object through one or more sample objects. If you do not specify an object to be extended, it is extended to yourself.
Jquery.extend can also be used by JQuery.fn.extend, which is used in jQuery to extend members to a target object, with extended members coming from a series of reference objects.
In this way, if we need to removedata the Jquery.fn extended member, we can do so.
Copy Code code as follows:
JQuery.fn.extend (
{
Removedata:function (key) {
Return This.each (function () {
Jquery.removedata (this, key);
});
}
}
);
Extend source code as follows, because it is simpler, so did not do too much streamlining.
Copy Code code as follows:
<reference path= "Jquery-core.js"/>
2
3
4 Jquery.extend = JQuery.fn.extend = function () {
5//Copy reference to target object
6 var target = Arguments[0] | | {}, I = 1, length = arguments.length, deep = False, options, name, SRC, copy;
7
8//Deep copy case, the first argument is a Boolean type, then, a deep copy, the second parameter is the target object
9 if (typeof target = = "Boolean") {
Deep = target;
target = Arguments[1] | | {};
Skip the Boolean and the target
i = 2;
}
If the target is not an object or a function
if (typeof target!== "Object" &&!jquery.isfunction (target)) {
target = {};
}
If there is only one argument is to expand yourself
if (length = = i) {
target = this;
I.;
}
Iterate through all the reference objects and extend to the target object
for (; i < length; i++) {
Only deal with non-null/undefined values
if (options = arguments[i])!= null) {
Extend the Base Object
for (name in options) {
src = target[name];
copy = Options[name];
Prevent never-ending Loop
if (target = = copy) {
Continue
}
recurse if we ' re merging object literal values or arrays
if (Deep && copy && (jquery.isplainobject (copy) | | jquery.isarray (copy)) {
var clone = src && (jquery.isplainobject (src) | | jquery.isarray (SRC))? Src
: Jquery.isarray (copy)? [] : {};
Never move original objects, clone them
Target[name] = Jquery.extend (deep, clone, copy);
Don ' t bring in undefined values
else if (copy!== undefined) {
Target[name] = copy;
}
}
}
}
Return the Modified Object
return target;
};