jQuery 原生代码编写:里面包含了样式、继承、实例方法、静态方法、处理原型对象、响应头部以及上下文参数

(function(){
var _$ = window.$;
var _jQuery = window.jQuery;
//暴露外部使用的一个接口
var jQuery = window.jQuery = window.$ = function(selector){

return new jQuery.fn.init(selector);
};


//处理原型对象
jQuery.fn = jQuery.prototype = {
init:function(selector){
var elements = document.getElementsByTagName(selector);
Array.prototype.push.apply(this,elements);
return this;
},
jQuery:"1.0.0",
length:0,
size:function(){
return this.length;
}

};
jQuery.fn.init.prototype = jQuery.fn;
//实现继承,并且只处理只有一个参数,也就是插件的扩展
jQuery.extend = jQuery.fn.extend = function(){
var o = arguments[0];
for(var p in o){
this[p] = o[p];
}
};

//添加静态方法
jQuery.extend({
trim:function(text){
return (text||"").replace(/^s+|s+$/g,"");
},
noConflict:function(){
window.$ = _$;
window.jQuery = _jQuery;
return jQuery;
}
});
//添加实例方法

jQuery.fn.extend({
get:function(num){
return this[num];
},
each:function(fn){
for(var i = 0 ;i< this.length; i++){
fn(i,this[i]);
}
return this;
},
//样式
css:function(){
var l = arguments.length;
if(l == 1){
return this[0].style[arguments[0]];
} else {
var name = arguments[0];
var value = arguments[1];
this.each(function(index,ele) {
ele.style[name] = value;

});
}
return this;
}

});
//样式的处理
jQuery.fn.extend({
css:function(name,value){
this[0].style(name) = value;
},
return this;
});

//覆盖响应内容类型的头部
jQuery.fn.extend({
overrideMimeType :function(type){
if(!state){
s.mimeType = type;
}
return this;
},

});
//用给定的上下文参数
jQuery.fn.extend({
resolveWith: function (context, args) {
if (!cancelled && !fired && !firing) {
args = args || [];
firing = 1;
try {
while (callbacks[0]) {
callbacks.shift().apply(context, args);
}
}
finally {
fired = [context, args];
firing = 0;
}
}
return this;
},
});

}

})();

多多指教,我还是前端小生
原文地址:https://www.cnblogs.com/yangslin/p/7900247.html