jQuery源码总结
来源:互联网 发布:大数据100g百度云 编辑:程序博客网 时间:2024/06/06 15:37
jQuery是大家都会使用的js库,因其方便性而广泛使用到个网址中。
jQuery核心代码结构模拟:
(function(window){ var A=function(ok){ //return this; return new A.fn.init(ok); }; A.fn=A.prototype={ init:function(ok){ if(!ok){ return this; } }, post:function(){ alert("my post"); }, get:function(){ alert("my get"); } }; A.fn.init.prototype=A.fn; A.extend=A.fn.extend=function(obj,content){ if(typeof(obj)=="object"){ for(var name in obj){ //alert(name); A.prototype[name]=obj[name]; } }else if(typeof(obj)=="function"){ A.prototype=obj.prototype; for(var name in content){ A.prototype[name]=content[name]; } } } window.L=A; })(window) //=============================== L().extend({ name:"liujian", love:function(){ alert("my love"); } }); L().love(); L().post();
- 首先通过闭包将window对象传人内部并通过window.L=A;将L 和A代表jQuery 和 $
- 通过A.fn.init.prototype=A.fn; 和A.fn=A.prototype 使A.fn.init A.fn A L这四个对象都可以调用函数库
- extend方法的模拟实现可以将自己的函数扩展到jQuery函数库
jquery源码对比:
基本框架:
(function(window){ "use strict"; var jQuery = window.jQuery = window.$ = function(selector){ //定义$函数,并把$和jQuery暴露到外面 }; jQuery.fn = jQuery.prototype = { //jQuery原型 }; jQuery.extend = jQuery.fn.extend = function(){ //添加扩展方法,jQuery.extend添加静态方法,也可以实现继承,jQuery.fn.extend添加动态方法 }})(window);
jQuery的初始化:
//由于$('#selector')得到的是一个jQuery对象,尝试直接返回jQuery对象var jQuery = window.jQuery = window.$ = function(selector){ return new jQuery(); //这里会导致一个死循环,所以不能这样直接构建jQuery对象};
init()
var jQuery = window.jQuery = window.$ = function(selector){ return new jQuery.fn.init(selector); //执行初始化};jQuery.fn = jQuery.prototype = { constructor: jQuery, init: function(selector){ var elements = document.querySelectorAll(selector); //顺便简单的实现了选择器 //注意querySelectorAll在老的浏览器中是不支持的,这里专注在jQuery的结构上。 Array.prototype.push.apply(this, elements); //构成类数组对象,引入length,并使其自增 return this; }, jQuery: "1.0.0", //jQuery版本信息 length: 0, //模拟数组,即这里构成一个类数组对象 size: function(){ return this.length; }}jQuery.fn.init.prototype = jQuery.fn;
extend方法
jQuery.extend = jQuery.fn.extend = function(obj, srcObj){ var target, len = arguments.length; if(len === 1){ //传入一个参数时实现继承 deep(obj, this); return this; } else { for(var i = 1; i < len; i++){ target = arguments[i]; deep(target, obj); } return obj; } function deep(oldOne, newOne){ //实现深拷贝 for(var prop in oldOne){ if(typeof oldOne[prop] === "object" && oldOne[prop] !== null){ newOne[prop] = oldOne[prop].constructor === Array ? [] : {}; deep(oldOne[prop], newOne[prop]); } else{ newOne[prop] = oldOne[prop]; } } } };
阅读全文
1 0
- jQuery源码部分总结
- jQuery源码总结
- Jquery源码中的小技巧总结
- jquery源码
- JQuery源码
- jQuery源码
- jquery源码
- jQuery源码
- jQuery源码
- jquery源码
- jQuery源码
- jQuery源码
- jquery源码-jquery.extend()
- [ jQuery ] jQuery 源码分析!
- jQuery源码-jQuery.extend
- Jquery总结
- JQuery总结
- jquery 总结
- 冒泡排序
- 数据结构---完全二叉树
- SpringMVC获取表单批量提交数据
- Django初识
- 三 树莓派开启samba服务
- jQuery源码总结
- 快排
- 67. Add Binary
- debian、ubuntu linux 源 汇总
- Spring Security(03)——核心类简介
- IP报文头
- USACO-Section1.2 Name That Number [其他]
- javascript中数据类型值的存储方式和位置
- PCA(主成分)分析及MATLAB实现