【21 => 94】变量和函数的定义

源码

var
    // rootjQuery = jQuery(document);
    rootjQuery,
  
    // The deferred used on DOM ready
    readyList,
  
    // Support: IE9
    // For `typeof xmlNode.method` instead of `xmlNode.method !== undefined`
    core_strundefined = typeof undefined,
  
    // Use the correct document accordingly with window argument (sandbox)
    location = window.location,
    document = window.document,
    docElem = document.documentElement,
  
    // Map over jQuery in case of overwrite
    _jQuery = window.jQuery,
  
    // Map over the $ in case of overwrite
    _$ = window.$,
  
    // [[Class]] -> type pairs

    class2type = {},
  
    // List of deleted data cache ids, so we can reuse them
    core_deletedIds = [],
  
    core_version = "2.0.3",
  
    // Save a reference to some core methods
    core_concat = core_deletedIds.concat,
    core_push = core_deletedIds.push,
    core_slice = core_deletedIds.slice,
    core_indexOf = core_deletedIds.indexOf,
    core_toString = class2type.toString,
    core_hasOwn = class2type.hasOwnProperty,
    core_trim = core_version.trim,
  
    // Define a local copy of jQuery
    jQuery = function( selector, context ) {
      // The jQuery object is actually just the init constructor 'enhanced'
      return new jQuery.fn.init( selector, context, rootjQuery );
    },
  
    // Used for matching numbers
    core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,
  
    // Used for splitting on whitespace
    core_rnotwhite = /\S+/g,
  
    // A simple way to check for HTML strings
    // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
    // Strict HTML recognition (#11290: must start with <)
    rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,
  
    // Match a standalone tag
    rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,
  
    // Matches dashed string for camelizing
    rmsPrefix = /^-ms-/,
    rdashAlpha = /-([\da-z])/gi,
  
    // Used by jQuery.camelCase as callback to replace()
    fcamelCase = function( all, letter ) {
      return letter.toUpperCase();
    },
  
    // The ready event handler and self cleanup method
    completed = function() {
      document.removeEventListener( "DOMContentLoaded", completed, false );
      window.removeEventListener( "load", completed, false );
      jQuery.ready();
    };

解析

rootjQuery
  • rootjQuery = jQuery(document)
  • 使用变量储存可以在压缩代码的时候减少代码量
  • 可以定义jQuery(document)的含义,在后期,容易维护

readyList

core_strundefined
  • core_strundefined = typeof undefined
  • typeof undefined = 'undefined'
  • 在高版本浏览器中,下面两种都是可以的
    window.a == undefined
    typeof window.a == 'undefined';
    
  • 在IE9和IE9之前的,都会存在这个问题
    假如判断的是typeof xmlNode.method,那么是不等于 undefined
    如果是使用 'undefined' 的话,高版本和低版本浏览器都是支持的

location = window.location,document = window.document,docElem = document.documentElement,
  • 进行一些变量的存储,有利于压缩的操作

_jQuery = window.jQuery, _$ = window.$,
<!-- 例如,防止之前有定义过$和jQuery变量 -->
<script>
  var $ = 10;
</script>
<script src="jquery-2.0.3.js"></script>
<script>
$() jQuery()
</script>

class2type
  • class2type = {}
  • $.type()中需要使用
  • class2type = { '[Object String]' : 'string', '[Object Array]' : 'array'}

储存对象、数组、字符串自带的一些方法
class2type = {},
  
// List of deleted data cache ids, so we can reuse them
// 跟缓存数据,删除id有关系
core_deletedIds = [],

core_version = "2.0.3",

// Save a reference to some core methods
core_concat = core_deletedIds.concat,
core_push = core_deletedIds.push,
core_slice = core_deletedIds.slice,
core_indexOf = core_deletedIds.indexOf,
core_toString = class2type.toString,
// 检测一个属性是否是对象的自有属性
/*
function F() {  //自定义数据类型
    this.name = "自有属性";
}
F.prototype.name = "继承属性";
*/
core_hasOwn = class2type.hasOwnProperty,
core_trim = core_version.trim,

正则表达式
// 数字的匹配: 包括了正负数和科学计数法的数学表示
core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,
  
// 匹配非空白
core_rnotwhite = /\S+/g,

// 表示的是html标签必须是: 
// <字符>字符
// #字符
rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,

// 匹配一个单独标准的html标签
// <span></span>
// <img src = "" />
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,

// 匹配Microsoft Edge浏览器
rmsPrefix = /^-ms-/,
// 是将(-字符)这个组合转换成大写字符
// -moz-transform -> MozTransform
rdashAlpha = /-([\da-z])/gi,

// 把字符串转化为大写
fcamelCase = function( all, letter ) {
  return letter.toUpperCase();
}

jQuery对象的初始化
// selector 储存选择的字符串
// context 表示在什么范围内选择
/ $('li', 'ul');
jQuery = function( selector, context ) {
  // The jQuery object is actually just the init constructor 'enhanced'
  return new jQuery.fn.init( selector, context, rootjQuery );
},
// 之前的解决方法
function Aaa(){

}
Aaa.prototype.init = function(){

}
Aaa.prototype.css = function(){
  
}
var a1 = new Aaa();
a1.init();
a1.css();

// jQuey的解决方法
function jQuery(){
  // 原型是对象
  return new jQuery.prototype.init()
}

jQuery.prototype.init = function(){

}

jQuery.prototype.css = function(){
  
}

jQuery.prototype.init.prototype = jQuery.prototype;
DOM 加载成功的时候触发的
completed = function() {
  document.removeEventListener( "DOMContentLoaded", completed, false );
  window.removeEventListener( "load", completed, false );
  jQuery.ready();
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值