这篇文章给大家分享的内容是关于如何搭建仿jquery的骨架并测试(附代码),有一定的参考价值,有需要的朋友可以从参考一下,希望对你有所帮助。
搭建了个仿jquery的骨架,jquery的骨架也差不多这样(function(global, factory) { if (typeof global.document === 'undefined') { throw new error('the environment must have a window object with document !') } // 若环境存在则执行factory factory(global);})(typeof window !== 'undefined' ? window : this, function (window) { var _mjq = function (selector) { return new _mjq.init(selector); } // 初始化 _mjq.init = function(selector) { // 进行selector匹配,比如class,attr,id等... if (selector === '#test') { const elem = document.getelementbyid('test') this.elem = elem return this } return this } // 让init的原型对象指向_mjq的原型 _mjq.init.prototype = _mjq.prototype = { // 功能 each: function() { // 循环 }, html: function() {}, css: function (name, value) { console.log(this) this.elem.style[name] = value } } // 设置contructor指向问题 object.defineproperty(_mjq.prototype, 'constructor', { enumerable: false, value: _mjq }) // 挂载到window window.$ = window.mjq = _mjq;})
测试demo地址https://github.com/clm960227/...
测试结果
相关文章推荐:
svg中<marker>元素的使用及marker属性的介绍
javascript设计模式之简单介绍适配器模式
angular表单验证的两种方法介绍
以上就是如何搭建仿jquery的骨架并测试(附代码)的详细内容。