栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

如何用不到200行代码写一款属于自己的js类库

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何用不到200行代码写一款属于自己的js类库

前言

Javascript 的核心是支持面向对象的,同时它也提供了强大灵活的 OOP 语言能力。本文将使用面向对象的方式,来教大家用原生js写出一个类似jQuery这样的类库。我们将会学到如下知识点:


闭包:减少变量污染,缩短变量查找范围

自执行函数在对象中的运用

extend的实现原理

如何实现跨浏览器的事件监听

原型链与继承

接下来我会对类库的核心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]) 案例如下

// 访问css 
Xuery('#demo').css('width') 

// 设置css 
Xuery('#demo').css('width', '1024px') 

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


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

// 访问attr 
Xuery('#demo').attr('title') 

// 设置attr 
Xuery('#demo').attr('title', '1024px') 

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


4、访问和设置html 案例如下:

// 访问 
Xuery('#demo').html() 

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

http://edu.duolaimier.cn

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

http://edu.duolaimier.cn

核心源码

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

 
(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=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);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/245325.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号