如何使用不到200行代碼寫一款js類庫

小編給大家分享一下如何使用不到200行代碼寫一款js類庫,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)建站是網(wǎng)站建設(shè)技術(shù)企業(yè),為成都企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、成都做網(wǎng)站,網(wǎng)站設(shè)計,網(wǎng)站制作,網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制適合企業(yè)的網(wǎng)站。10年品質(zhì),值得信賴!

類庫設(shè)計思路

如何使用不到200行代碼寫一款js類庫

API介紹和效果展示

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

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

2、訪問和設(shè)置css Xuery.css(string|object, ?[string])案例如下:

// 訪問css
Xuery('#demo').css('width')
// 設(shè)置css
Xuery('#demo').css('width', '1024px')
// 設(shè)置css
Xuery('#demo').css({
 width: '1024px',
 height: '1024px'
})

3、訪問和設(shè)置屬性 Xuery.attr(string|object, ?[string])案例如下:

// 訪問attr
Xuery('#demo').attr('title')
// 設(shè)置attr
Xuery('#demo').attr('title', '1024px')
// 設(shè)置attrs
Xuery('#demo').attr({
 title: '1024px',
 name: '1024px'
})

4、訪問和設(shè)置html案例如下:

// 訪問
Xuery('#demo').html()
// 設(shè)置
Xuery('#demo').html('前端學(xué)習(xí)原生框架')

還有其他幾個常用的API在這里就不介紹了,大家可以在我的github上查看,或者基于這套基礎(chǔ)框架,去擴(kuò)展屬于自己的js框架。

核心源碼

以下源碼相關(guān)功能我做了注釋,建議大家認(rèn)真閱讀,涉及到原型鏈和構(gòu)造函數(shù)的指向的問題,是實現(xiàn)上述調(diào)用方式的核心,又不懂可以在評論區(qū)交流溝通。

/**
 * 鏈模式實現(xiàn)自己的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) {
  // 設(shè)置元素長度
  this.length = 0;
  // 默認(rèn)獲取元素的上下文document
  context = context || document;
  // id選擇符,則按位非將-1轉(zhuǎn)化為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
 },
 // 增強數(shù)組
 push: [].push,
 sort: [].sort,
 splice: [].splice
 };

 // 方法擴(kuò)展
 Xuery.extend = Xuery.fn.extend = function(){
 // 擴(kuò)展對象從第二個參數(shù)算起
 var i = 1,
 len = arguments.length,
 target = arguments[0],
 j;
 if(i === len){
  target = this;
  i--;
 }
 // 將參數(shù)對象合并到target
 for(; i<len; i++){
  for(j in arguments[i]){
  target[j] = arguments[i][j];
  }
 }
 return target
 }

 // 擴(kuò)展事件方法
 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
  }
  }
 })()
 })

 // 將‘-'分割線轉(zhuǎn)換為駝峰式
 Xuery.extend({
 camelCase: function(str){
  return str.replace(/\-(\w)/g, function(all, letter){
  return letter.toUpperCase();
  })
 }
 })

 // 設(shè)置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
 }
 })

 // 設(shè)置屬性
 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
 }
 })

 // 獲取或者設(shè)置元素內(nèi)容
 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);

以上是“如何使用不到200行代碼寫一款js類庫”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章標(biāo)題:如何使用不到200行代碼寫一款js類庫
分享網(wǎng)址:http://bm7419.com/article26/pcoejg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、定制網(wǎng)站網(wǎng)站建設(shè)、面包屑導(dǎo)航Google、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管