Home >Web Front-end >JS Tutorial >JQuery1.6 usage method three_jquery
a={id:1,get:function(){alert(this.id)}}
$("#test").click(a.get)//The id referred to at this time is not 1. But test
If you want to get 1, you must change the context $.
The function of $.proxy() is to change the current context.
$("#test").click($.proxy(a,"get"))
$("#test").click($.proxy(a.get,a))
proxy: function( fn, context ) {//Change the function context, this points to the set object
The parameters here can be in two ways: function fn fn method context
Function method fn[context] Function fn
if ( typeof context === "string" ) {//If content is a string, it will be considered as a method fn[ context ] of fn, and then determine whether it is a function
var tmp = fn[ context ];
context = fn;
fn = tmp;
}
if ( !jQuery.isFunction( fn ) ) {//Not a function that returns undefined immediately
return undefined;
}
var args = slice.call( arguments, 2),//Use the parameters after fn and content as the function parameters to be executed
proxy = function() {
return fn .apply( context, args.concat( slice.call( arguments ) ) );//Return the executable function with the specified context set, fn is the number of executable lines, point this to content, args.concat( slice.call (arguments)) as parameters,
};
proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid;//A global counter used to identify that the function can Used to delete
return proxy;
},
//access This method is mainly used inside jquery and is used for attr, prop, css; it mainly sets single or multiple attributes of the DOM group (jquery object) , the value of the style, get the value of a certain attribute and style of the DOM group (jquery object)
//The object group operated by elems, key attribute, value attribute value, exec defaults to true, the function used by fn to operate, pass is the main When setting val, css, html, text, data, width, height, and offset in attr, it should be true.
access: function(elems, key, value, exec, fn, pass) {
var length = elems.length;
//Set multiple attributes
if ( typeof key === "object" ) {//If the attribute value is an object, it will loop to set the attributes of the dom group (jquery object) and Corresponding value
for ( var k in key ) {
jQuery.access( elems, k, key[k], exec, fn, value );
}
return elems;//return Result
}
//Set a single attribute
if (value !== undefined) {//If value exists, set a certain attribute, which can be a group of objects or a single one; if it does not exist, get it doms[0] (jquery object) A certain attribute value can only return an object, or directly return undefiend because the object does not exist;
exec = !pass && exec && jQuery.isFunction(value);//currently jquery The default pass is null, exec is true, whether value is a function
for ( var i = 0; i fn( elems[i], key, exec ? value.call( elems [i], i, fn( elems[i], key ) ) : value, pass );//If exec is true, pass in the sequence and operation attribute value of the operation object as parameters, otherwise, set the value of the attribute key For value
}
return elems;
}
// Get attribute
return length ? fn( elems[0], key ) : undefined;
},
now : function() {//Return the current time
return (new Date()).getTime();
},