当前位置: 代码迷 >> Web前端 >> JQuery容易模仿
  详细解决方案

JQuery容易模仿

热度:337   发布时间:2012-11-23 22:54:33.0
JQuery简单模仿
转 http://www.cnblogs.com/baochuan/archive/2012/04/30/2473771.html


//模仿JQuery的方式
(function(){
	var yQuery = (function(){
		var yQuery = function(){
			return yQuery.fn.init();
		};
		yQuery.fn = yQuery.prototypy = {
			init : function(){
				return this;
			}
		}
		yQuery.extend = yQuery.fn.extend = function(){
			var options , name , src , copy,
				target = arguments[0] || {},
				i = 1,
				length = arguments.length;
				
				if(length === i){
					target = this;
					--i;
				}
				
				for(; i < length ; i ++ ){
					if((options = arguments[i]) != null){
						for(name in options){
							src = target[name];
							copy = options[name];
						
							if(src === copy){//检查在目标对象中src属性这个值是否一致
								continue;
							}
							if(copy!==undefined){
								target[ name ] = copy;
							}
						}
					}
				}
				return target;
		}
		return yQuery;
	})();
	window.yQuery = window.$ = yQuery();
})();

$.ui = $.ui || {};
$.extend($.ui , {name2:"eee" , getName:function(){return this.name2;}});
console.log($.ui.getName());




上面这个不写成下面这个的原因 不是很明白


(function(){
	var jQuery = function(){
		return jQuery.fn.init();
	};
	jQuery.fn = jQuery.prototype = {
		init : function(){ return this;}
	};
	
	jQuery.fn.extend = function(){
	
		var src , copy , name , options ,
			target = arguments[0] || {};
			i = 1,
			length = arguments.length;
			if(length===i){
				target = this;
				i--;
			}
			for( ; i <length ; i++){
				if((options = arguments[i])!=null){
					for(name in options){
						src = options[ name ];
						copy = target[ name ];
						
						if(src===copy){
							continue;
						}
						if(src!==undefined){
							target[ name ] = src;
						}
					}
				}
			}
		return target;
	};
	window.jQuery = window.$ = jQuery();
	return jQuery;
})();

$.ui = $.ui || {};
$.extend($.ui , {name2:"eee"}, {getName:function(){return this.name2}});
console.log($.ui.getName());



  相关解决方案