JQuery源码解析-- 对象的创建
2024-10-16 16:25:32
使用 $("a") 返回的对象就不再是一个简单的DOM对象了,而是一个复杂的JQuery对象。
那么JQuery是怎么创建对象的。
为了便于分析,我将JQuery中复杂的代码简化了一番,便于分析。
(function(global,factory){
factory(global);
})(this,function(window,noGlobal){
var myJQuery=function(selector,context){
return new myJQuery.fn.init(selector,context);
};
myJQuery.fn=myJQuery.prototype={
constructor:myJQuery,//这里非常重要
length: 0,
each:function(){
console.log("创建了一个JQuery对象"+this.targetName);
}
};
var init=myJQuery.fn.init=function(selector,context,root){
return this;
};
init.prototype = myJQuery.fn;//
if(!noGlobal){
window.myJQuery=window.F$=myJQuery;
}
return myJQuery;
});
调用时,只需要 F$("div").each(); 即可模拟JQuery的对象穿件。
整个JQuery库是一个立即执行函数。
init.prototype = myJQuery.fn; 这句话十分重要。
最新文章
- iOS-绘图
- XmlUtils.java
- 你真的知道setTimeout是如何运行的吗
- windows的Timer和写文件方式串口注意!
- Codeforces 723e [图论][欧拉回路]
- JS模态窗口返回值兼容问题解决方案
- HTML5 game engines
- inno setup 执行SQL
- gdb调试整理
- Selenium+Python ---- 免登录、等待、unittest单元测试框架、PO模型
- jQuery中mouseleave和mouseout的区别详解
- ASP.NET WebApi 基于分布式Session方式实现Token签名认证
- Spark性能优化指南——基础篇
- SAP Brazil J1BTAX 为税收例外创建税收组(翻译)
- [jshint] 'import' is only available in ES6 (use 'esversion: 6'). (W119) 提示import等ES6语法的jshint错误的,在代码前加一行 /* jshint esversion: 6 */
- ThinkPHP redirect 传参
- [NOI2003]Editor &; [AHOI2006]文本编辑器editor BZOJ1507&;BZOJ1269
- layout图形化界面看不到内容 Failed to find the style corresponding to the id
- Java8 Lambda 之 Collection Stream
- HDOJ1007