使用 $("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; 这句话十分重要。

  

最新文章

  1. iOS-绘图
  2. XmlUtils.java
  3. 你真的知道setTimeout是如何运行的吗
  4. windows的Timer和写文件方式串口注意!
  5. Codeforces 723e [图论][欧拉回路]
  6. JS模态窗口返回值兼容问题解决方案
  7. HTML5 game engines
  8. inno setup 执行SQL
  9. gdb调试整理
  10. Selenium+Python ---- 免登录、等待、unittest单元测试框架、PO模型
  11. jQuery中mouseleave和mouseout的区别详解
  12. ASP.NET WebApi 基于分布式Session方式实现Token签名认证
  13. Spark性能优化指南——基础篇
  14. SAP Brazil J1BTAX 为税收例外创建税收组(翻译)
  15. [jshint] 'import' is only available in ES6 (use 'esversion: 6'). (W119) 提示import等ES6语法的jshint错误的,在代码前加一行 /* jshint esversion: 6 */
  16. ThinkPHP redirect 传参
  17. [NOI2003]Editor & [AHOI2006]文本编辑器editor BZOJ1507&BZOJ1269
  18. layout图形化界面看不到内容 Failed to find the style corresponding to the id
  19. Java8 Lambda 之 Collection Stream
  20. HDOJ1007

热门文章

  1. Ambari 不能配置 Kafka 监听host的问题
  2. Win10光驱不见了
  3. Unable to install breakpoint in
  4. 腾讯云Centos下Nginx反向代理Apache+Tomcat
  5. onethink入门笔记(一)
  6. STL之序列容器vector
  7. cursor 鼠标样式——属性
  8. 使用es6的set和map实现数组去重复
  9. MES系统学习
  10. java闭包