Giter Club home page Giter Club logo

learn-jquery's Introduction

jquery源码解读

简化jquery的源码:

( function( global, factory ) {
	//...兼容环境
})( typeof window !== "undefined" ? window : this, function( window, noGlobal )

其中,factory函数是jquery对象的实例化。

对于global对象,在浏览器内是为window,而在Node环境内,是global,因此进行如下判断:

// 当为Node环境时,this === global
// 判断 window 对象是否存在,若否则代入全局的 this,即 Node 环境下的 global 对象
typeof window !== "undefined" ? window : this, function( window, noGlobal )

jquery 兼容环境的做法为:

function( global, factory ) {

	"use strict";
	//判断是否支持CommonJs类的环境
	if ( typeof module === "object" && typeof module.exports === "object" ) {

		// 对于CommonJS和CommonJS类环境,其中有一个正确的“窗口”
		// 出现,执行工厂并获得jQuery。
		// 对于没有“文件”的“窗口”的环境
		// (如Node.js),将工厂公开为module.exports
		// 这突出了创建一个真正的“窗口”的需要
		module.exports = global.document ?
			factory( global, true ) :
			function( w ) {
				if ( !w.document ) {
					throw new Error( "jQuery requires a window with a document" );
				}
				return factory( w );
			};
	} else {
		factory( global );
	}
}

流程图如下:

Alt text

其中,

if ( typeof module === "object" && typeof module.exports === "object" )

是为了兼容CommonJS规范的框架。

isWindow函数

源码:

var isWindow = function isWindow( obj ) {
		return obj != null && obj === obj.window;
	};

判断是否为window对象,如果是window对象时,window === window.window

DOMEval函数

源码:

	function DOMEval( code, doc ) {
		doc = doc || document;
		var script = doc.createElement( "script" );
		script.text = code;
		doc.head.appendChild( script ).parentNode.removeChild( script );
	}

功能:其实跟jseval函数很相像,就是执行一端js代码。

学习了:

appendChild => scripr.text => removeChild => parentNode =>

jQuery函数

源码:

jQuery = function( selector, context ) {
	return new jQuery.fn.init( selector, context );
}

功能:返回jq对象,jq对象有内置一些功能。

toArray函数

源码:

function() {
	return slice.call( this );
}

功能:类数组转换成数组

get函数

get: function( num ) {

		if ( num == null ) {
			return slice.call( this );
		}

		return num < 0 ? this[ num + this.length ] : this[ num ];
	}

learn-jquery's People

Contributors

lixinz avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.