In some html, we often see the use of plug-ins:
For example:
$ ('# Slides'). slides ({
Preload: true,
Container: 'slides _ iner ',
SlidesLoaded: function (){
JQuery ('. caption'). fadeIn (800 );
}
});
This is the api interface provided by a slides function. It encapsulates a js function and provides some interfaces: preload, container, slidesLoaded,
In this way, you only need to input the corresponding value according to the interface instructions to run this function normally.
JQuery is a huge function.
The simple principle is to use extend to merge object content functions.
In this function, slides () also has a default object:
JQuery. fn. slides. option = {
Preload: false,
Container: 'slidescontainer ',
SlidesLoaded: function (){},
};
When no external value is passed in for specific use, execute the default object, pass in the content, and execute the input parameters:
Use: option = jquery. Extend ({}, jquery. FN. Slides. Option, option );
Merge two objects:
Jquery. FN. Slides. option is the default attribute.
Option is the attribute passed in when this function is referenced. option is later and the attribute takes precedence.
Merge attribute syntax
Jquery. Extend (target, [object1], [objectn])
Target: an object. If the attached object is passed to this method, it receives new attributes. If it is a unique parameter, it extends the jquery namespace.
Object1: an object that includes additional attributes and is merged to the first parameter.
Objectn: Includes additional attributes merged to the first parameter.
When we provide two or more objects to $. Extend (), all attributes of the object are added to the target object.
If only one parameter is provided to $. Extend (), this means that the target parameter is omitted. In this case, jquery objects are set as targets by default. In this way, we can add new functions under the namespace of jquery. This can be used by the plug-in author to add a new method to jquery.
Remember that the target object (the first parameter) will be modified and will be returned through $. Extend. However, if we want to retain the original object, we can pass an empty object as the target:
VaR object = $. Extend ({}, object1, object2 );
By $. extend () Merge execution is not recursive by default. If the attribute of the first object itself is an object or array, this will completely rewrite an attribute with the same key of the second object. These values are not merged. In the following example, we can see that the banana value is checked. However, true is the first function parameter, and the object is recursively merged.
Undefined attributes are not copied. However, inherit attributes from the object prototypeSetCopied.
Instance:
Var empty = {}
Var defaults = {validate: false, limit: 5, name: "foo "};
Var options = {validate: true, name: "bar "};
Var settings = $. extend (empty, defaults, options );
Settings = {validate: true, limit: 5, name: "bar "}
Empty = {validate: true, limit: 5, name: "bar "}