小记jQuery扩展 》初始化,流程

[javascript] // Define a local copy of jQuery var jQuery = $ = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' return new jQuery.fn.init( selector, context ); } jQuery.fn = jQuery.prototype = { init: function(selector, context){} }; // Give the init function the jQuery prototype for later instantiation jQuery.fn.init.prototype = jQuery.fn; jQuery.extend = jQuery.fn.extend = function() { // copy reference to target object var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy; // Handle a deep copy situation if ( typeof target === "boolean" ) { deep = target; target = arguments[1] || {}; // skip the boolean and the target i = 2; } // Handle case when 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 ( 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; }; //------------------------------------------------------------------------------- //原始扩展方法是放在沙箱中处理的,这样保证每个实例对象都可以调用到原始扩展方法 jQuery.fn.extend({ fuckjQuery_fn: function( key ) { return this.each(function() { jQuery.removeData( this, key ); }); } }); jQuery.extend({ fuckjQuery: function( key ) { return this.each(function() { jQuery.removeData( this, key ); }); } }); //------------------------------------------------------------------------------- console.dir($) console.log('---------------华丽丽的分隔线---------------'); console.dir($()) [/javascript] //自绘草图一张
posted @ 2010-08-17 10:20  7hihi  阅读(221)  评论(0编辑  收藏  举报