前言
JavaScript 的核心是支持面向對象的,同時它也提供了強大靈活的 OOP 語言能力。本文將使用面向對象的方式,來教大家用原生js寫出一個類似jQuery這樣的類庫。我們將會學到如下知識點:
接下來我會對類庫的核心api進行講解和展示,文章最后后附帶類庫的完整源碼,在我之前的文章《3分鐘教你用原生js實現具有進度監聽的文件上傳預覽組件》中也使用了類似的方式,感興趣的可以一起學習,交流。
更加完整的類庫地址,請移步github《Xuery――仿jquery API風格的輕量級可擴展的原生js框架》(本地下載)
類庫設計思路

API介紹和效果展示
1、事件綁定 Xuery.on(eventName, fn)案例如下:
Xuery('#demo').on('click', function(e){ alert('hello world!')})2、訪問和設置css Xuery.css(string|object, ?[string])案例如下:
// 訪問cssXuery('#demo').css('width')// 設置cssXuery('#demo').css('width', '1024px')// 設置cssXuery('#demo').css({ width: '1024px', height: '1024px'})3、訪問和設置屬性 Xuery.attr(string|object, ?[string])案例如下:
// 訪問attrXuery('#demo').attr('title')// 設置attrXuery('#demo').attr('title', '1024px')// 設置attrsXuery('#demo').attr({ title: '1024px', name: '1024px'})4、訪問和設置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 } } })() }) // 將‘-'分割線轉換為駝峰式 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); 總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。
新聞熱點
疑難解答