(function( window, undefined ){
var jQuery = (function(){
var jQuery = function( selector, context ){
return new jQuery.fn.init( selector, context )
} jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context ){
if (!selector) return this;
var nodeList = (context || document).querySelectorAll( selector ),
i = 0,
length = nodeList.length;
for (; i<length; i+=1){
this[i] = nodeList[i];
}
this.length = nodeList.length;
return this;
}
}
//用fn的真正原因是
//如果不用fn,直接用jQuery.init 会返回undefined,主要是jQuery并不是new的对象,也就不能//访问原型,undefined不能设置原型
//jQuery.fn = {}可以直接访问init属性,所以fn必须写
console.log( jQuery.fn ); jQuery.fn.init.prototype = jQuery.fn; jQuery.fn.extend = jQuery.extend = function(obj){
var obj = obj || null;
if (!obj) return this;
for ( var i in obj){
if (!jQuery.fn[i]){
jQuery.fn[i] = obj[i];
}
}
return this;
} return jQuery;
})()
window.$ = window.jQuery = jQuery;
})( window, undefined )

下边是没有使用jQuery.fn的代码,使用了jQuery.fn的框架更简单,不用函数运行就可以对原型添加方法,也就是这一点,

因为直接jQuery.prototype不能直接添加方法,必须声明!但是jQuery.fn就可以,因为他不是原型而是一个{}object

jQuery.fn = jQuery.prototype = {};

<body>

    <h1>标题1</h1>
<h1>标题2</h1>
<h1>标题3</h1>
<script> var jQuery = function( selector, context ){
return new jQuery.prototype.init( selector, context );
} jQuery.prototype = { constructor: jQuery, init: function( selector, context ){ if ( !selector ) return;
var nodeList = ( context || document ).getElementsByTagName( selector ),
i = 0,
length = nodeList.length;
for (; i<length; i+=1){
this[i] = nodeList[i]
}
this.length = nodeList.length; return this
} } jQuery.prototype.init.prototype = jQuery.prototype;
jQuery.prototype.extend = function( obj ){
if ( !obj ) return;
for ( var i in obj ){
if ( !jQuery.prototype[i] ){
jQuery.prototype[i] = obj[i]
}
}
} jQuery.prototype.extend({
changeColor: function( color ){
if ( !color ) return;
var i = 0,
length = this.length;
for (; i<length; i+=1){
this[i].style.color = color;
}
return this;
}
});
jQuery('h1').changeColor('red');
//标准的写法 jQuery.fn.extend = jQuery.extend
//因为非标准的没有jQuery.fn 所以不能jQuery.extend()直接添加,必须jQuery.prototype.extend()
//进行添加,又因为jQuery.fn = jQuery.prototype = {},所以jQuery.fn是一个对象不用函数调用可以直//接添加
</script>
</body>

jQuery源码框架fn解读的更多相关文章

  1. jQuery源码笔记(一):jQuery的整体结构

    jQuery 是一个非常优秀的 JS 库,与 Prototype,YUI,Mootools 等众多的 Js 类库相比,它剑走偏锋,从 web 开发的实用角度出发,抛除了其它 Lib 中一些中看但不实用 ...

  2. jquery源码解读

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐进增强)优雅的处理能 ...

  3. jQuery源码中的“new jQuery.fn.init()”什么意思?

    所有文章搬运自我的个人主页:sheilasun.me 引子 最近打算试试看看jQuery的源码,刚开个头就卡住了.无论如何都理解不了jQuery源码入口部分中的 return new jQuery.f ...

  4. jQuery源码分析之整体框架

    之前只是知道jQuery怎么使用,但是我觉得有必要认真的阅读一下这个库,在分析jQuery源码之前,很有必要对整个jQuery有个整体的框架概念,才能方便后面对jQuery源码的分析和学习,以下是我总 ...

  5. 三.jQuery源码解析之jQuery的框架图

    这张图片是对jQuery源码截图,一点一点拼出来的. 现在根据这张图片来对jQuery框架做一些说明. 一.16~9404行可以发现,最外层是一个自调用函数.当jQuery初始化时,这个自调用函数包含 ...

  6. 读jQuery源码之整体框架分析

    读一个开源框架,大家最想学到的就是设计的思想和实现的技巧.最近读jQuery源码,记下我对大师作品的理解和心得,跟大家分享,权当抛砖引玉. 先附上jQuery的代码结构. (function(){ / ...

  7. jQuery源码解读----part 2

    分离构造器 通过new操作符构建一个对象,一般经过四步: A.创建一个新对象 B.将构造函数的作用域赋给新对象(所以this就指向了这个新对象) C.执行构造函数中的代码 D.返回这个新对象 最后一点 ...

  8. jQuery源码-dom操作之jQuery.fn.html

    写在前面 前面陆陆续续写了jQuery源码的一些分析,尽可能地想要cover里面的源码细节,结果导致进度有些缓慢.jQuery的源码本来就比较晦涩,里面还有很多为了解决兼容问题很引入的神代码,如果不g ...

  9. jQuery 源码解析二:jQuery.fn.extend=jQuery.extend 方法探究

    终于动笔开始 jQuery 源码解析第二篇,写文章还真是有难度,要把自已懂的表述清楚,要让别人听懂真的不是一见易事. 在 jQuery 源码解析一:jQuery 类库整体架构设计解析 一文,大致描述了 ...

随机推荐

  1. 调试HDF0308-A50的相机驱动。

    使用rk3128做为主芯片: 使用andriod5.1-sdk软件包. 1.在rk3128-86v.dts 中加入头文件 #include "rk3128-cif-sensor.dtsi&q ...

  2. Java8内置的函数式编程接口应用场景和方式

    首先,我们先定义一个函数式编程接口 @FunctionalInterface public interface BooleanFunctionalInterface<T> { boolea ...

  3. el-input的color修改无效问题

    相信很多前端初学者跟我一样也遇到过el-input的color修改无效问题 如下图:我想把el-input里面的文字改成蓝色,但是使用总是失败 修改方法:打开调试界面,找到el-input对应的sty ...

  4. JQUERY之表单验证案例

    <!-- 需求: 用户注册页面要有用户名.密码.确认密码.邮箱 用户名文本框:用户名不能为空,且必须为数字与字母的6到12位的组合 密码框:密码不能为空,六到八位数字或字母的组合 确认密码框:确 ...

  5. c#遍历一个对象中所有的属性和值

    SpDictItem sp = GetCFHObject.GetSpItem("); PropertyInfo[] propertys = sp.GetType().GetPropertie ...

  6. centOS7 关闭swap

    [root@cdh- sbin]# free -g total used free shared buff/cache available Mem: Swap: [root@cdh- sbin]# c ...

  7. pl/sql developer 问题总结

    问题1,出现NLS_LANG和字符集(Character set)问题 安装完PL/SQL后打开,遇到如图问题. 原因:这是因为系统没有设置NLS_LANG系统变量. 解决方法:有两种方式查看. 1. ...

  8. Harry Potter and J.K.Rowling(半平面交+圆和矩形交)

    Harry Potter and J.K.Rowling http://acm.hdu.edu.cn/showproblem.php?pid=3982 Time Limit: 2000/1000 MS ...

  9. java中解析excel 批量插入数据库

    Facade 层 实现类 (@Service("samePeriodModelImportFacade")) 1.  获取cells 的方法 public Cells getCel ...

  10. 计算C#程序执行时间

    static void SubTest()   {       DateTime beforDT = System.DateTime.Now;            //耗时巨大的代码         ...