jquery中被誉为工厂函数的是什么?

互联网 20-11-25

jquery中被誉为工厂函数的是“$()”,它本质上就是一个DOM对象,但是它所使用的方法都封装在了jQuery上,所以我们不能通过“$()”来使用JavaScript的方法,同样DOM对象也不能使用jQuery上的方法。

相关推荐:《jQuery教程》

jquery中被誉为工厂函数的是“$()”。在jQuery中,无论我们使用哪种类型的选择符都需要从一个“$”符号和一对“()”开始。

“$”是jQuery“类”的一个别称,$()构造了一个jQuery对象;所以,“$()”可以叫做jQuery的构造函数。

工厂函数“$()”本质上就是一个DOM对象,但是它所使用的方法都封装在了jQuery上,所以我们不能通过“$()”来使用JavaScript的方法,同样DOM对象也不能使用jQuery上的方法。

我们以$为开始,引出整个jQuery的架构

以jQuery的1.11.3版本举例,$作为一个函数名出现的地方是在源码的最后:

	window.jQuery = window.$ = jQuery;

其中的jQuery是前面定义的一个函数,在源码第70行中出现

	jQuery = function( selector, context ) { 		// The jQuery object is actually just the init constructor 'enhanced' 		// Need init if jQuery is called (just allow error to be thrown if not included) 		return new jQuery.fn.init( selector, context ); 	}

在源码第2882行中:

	// Give the init function the jQuery prototype for later instantiation 	init.prototype = jQuery.fn;
	jQuery.fn = jQuery.prototype
	jQuery.fn.alertMsg = function(msg){ 		alert('msg'); 	}

使用:

	$().alertMsg('Hello World!');

jQuery的整体架构到这里就差不多了

下面是一个简化版的jQuery架构,便于理解

	(function () { 	    function jQuery(selector) { 	        return new jQuery.prototype.init(selector); 	    } 	    // jQuery对象的构造函数 	    jQuery.prototype.init = function (selector) { 	        	    } 	    // jQuery原型上的css方法 	    jQuery.prototype.css = function (config) { 	         	    } 	    // 将jQuery原型上的方法都放到init的原型链上 	    jQuery.prototype.init.prototype = jQuery.prototype; 	    window.$ = window.jQuery = jQuery; 	})();

关系图解:

更多编程相关知识,请访问:编程学习网站!!

以上就是jquery中被誉为工厂函数的是什么?的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: jQuery
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:jquery中如何数组去重复数据

相关资讯