温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么写一款属于自己的JS类库

发布时间:2021-11-06 14:11:08 来源:亿速云 阅读:124 作者:iii 栏目:web开发

这篇文章主要讲解了“怎么写一款属于自己的JS类库”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么写一款属于自己的JS类库”吧!

API介绍和效果展示

事件绑定 Xuery.on(eventName, fn) 案例如下:

Xuery('#demo').on('click', function(e){     alert('hello world!') })

访问和设置css Xuery.css(string|object, ?[string]) 案例如下:

// 访问css Xuery('#demo').css('width') // 设置css Xuery('#demo').css('width', '1024px') // 设置css Xuery('#demo').css({     width: '1024px',     height: '1024px' })

访问和设置属性 Xuery.attr(string|object, ?[string]) 案例如下:

// 访问attr Xuery('#demo').attr('title') // 设置attr Xuery('#demo').attr('title', '1024px') // 设置attrs Xuery('#demo').attr({     title: '1024px',     name: '1024px' })

访问和设置html 案例如下:

// 访问 Xuery('#demo').html() // 设置 Xuery('#demo').html('前端学习原生框架')

还有其他几个常用的API在这里就不介绍了,大家可以在我的github上查看,或者基于这套基础框架,去扩展属于自己的js框架。

核心源码

以下源码相关功能我做了注释,建议大家认真阅读,涉及到原型链和构造函数的指向的问题,是实现上述调用方式的核心,又不懂可以在评论区交流沟通。

/**  * 链模式实现自己的js类库  */ (function(win, doc){     var Xuery = function(selector, context) {         return new Xuery.fn.init(selector, context)     };      Xuery.fn = Xuery.prototype = {     constructor: Xuery,     init: function(selector, context) {         // 设置元素长度         this.length = 0;         // 默认获取元素的上下文document         context = context || document;         // id选择符,则按位非将-1转化为0         if(~selector.indexOf('#')) {         this[0] = document.getElementById(selector.slice(1));         this.length = 1;         }else{         // 在上下文中选择元素         var doms = context.getElementsByTagName(selector),         i = 0,         len = doms.length;         for(; i<len; i++){             this[i] = doms[i];         }         }         this.context = context;         this.selector = selector;         return this     },     // 增强数组     push: [].push,     sort: [].sort,     splice: [].splice     };      // 方法扩展     Xuery.extend = Xuery.fn.extend = function(){     // 扩展对象从第二个参数算起     var i = 1,     len = arguments.length,     target = arguments[0],     j;     if(i === len){         target = this;         i--;     }     // 将参数对象合并到target     for(; i<len; i++){         for(j in arguments[i]){         target[j] = arguments[i][j];         }     }     return target     }      // 扩展事件方法     Xuery.fn.extend({     on: (function(){         if(document.addEventListener){         return function(type, fn){             var i = this.length -1;             for(; i>=0;i--){             this[i].addEventListener(type, fn, false)             }             return this         }         // ie浏览器dom2级事件         }else if(document.attachEvent){         return function(type, fn){             var i = this.length -1;             for(; i>=0;i--){             this[i].addEvent('on'+type, fn)             }             return this         }         // 不支持dom2的浏览器         }else{         return function(type, fn){             var i = this.length -1;             for(; i>=0;i--){             this[i]['on'+type] = fn;             }             return this         }         }     })()     })      // 将&lsquo;-&rsquo;分割线转换为驼峰式     Xuery.extend({     camelCase: function(str){         return str.replace(/\-(\w)/g, function(all, letter){         return letter.toUpperCase();         })     }     })      // 设置css     Xuery.extend({     css: function(){         var arg = arguments,         len = arg.length;         if(this.length < 1){         return this         }         if(len === 1) {         if(typeof arg[0] === 'string') {             if(this[0].currentStyle){             return this[0].currentStyle[arg[0]];             }else{             return getComputedStyle(this[0], false)[arg[0]]             }         }else if(typeof arg[0] === 'object'){             for(var i in arg[0]){             for(var j=this.length -1; j>=0; j--){                 this[j].style[Xuery.camelCase(i)] = arg[0][i];             }             }         }         }else if(len === 2){         for(var j=this.length -1; j>=0; j--){             this[j].style[Xuery.camelCase(arg[0])] = arg[1];         }         }         return this     }     })      // 设置属性     Xuery.extend({     attr: function(){         var arg = arguments,         len = arg.length;         if(len <1){         return this         }         if(len === 1){         if(typeof arg[0] === 'string'){             return this[0].getAttribute(arg[0])         }else if(typeof arg[0] === 'object'){             for(var i in arg[0]){             for(var j=this.length -1; j>= 0; j--){                 this[j].setAttribute(i, arg[0][i])             }             }         }         }         else if(len === 2){         for(var j=this.length -1; j>=0; j--){             this[j].setAttribute(arg[0], arg[1]);         }         }         return this     }     })      // 获取或者设置元素内容     Xuery.fn.extend({     html: function(){         var arg = arguments,         len = arg.length;         if(len === 0){         return this[0] && this[0].innerHTML         }else{         for(var i=this.length -1; i>=0; i--){             this[i].innerHTML = arg[0];         }         }         return this     }     })      Xuery.fn.init.prototype = Xuery.fn;     window.Xuery = Xuery; })(window, document);

感谢各位的阅读,以上就是“怎么写一款属于自己的JS类库”的内容了,经过本文的学习后,相信大家对怎么写一款属于自己的JS类库这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI