我是如何编写一个js插件的

编写插件的目的

  • 更好的复用
  • 方便维护
  • 支持配置
插件1.0
;(function (global) {
    //上传配置
    var config = {
        getTokenUrl: "//xxx.xxx.com/api/Active/qintoken?bucket=wnlother", //获取token接口地址
        qiniuUpUrl: "//xxx.qiniup.com/putb64/-1", //上传的七牛服务器地址
        imgUrlDomain: 'https://xxx.xxx.com/' // 图片域名
    };
    //定义上传方法
    function uploadQN(img, resolve) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', config.getTokenUrl, true);
        xhr.onreadystatechange = function() {
            // readyState == 4说明请求已完成
            if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) {
                // 从服务器获得数据 
                var res = JSON.parse(xhr.responseText);
                var xhr2 = new XMLHttpRequest();
                xhr2.open('POST', config.qiniuUpUrl, true);
                xhr2.setRequestHeader('Content-Type', 'application/octet-stream');
                xhr2.setRequestHeader('Authorization', 'UpToken ' + res.token);
                xhr2.send(img.substring(23));
                xhr2.onreadystatechange = function() {
                    if (xhr2.readyState === 4) {
                        var resData = JSON.parse(xhr2.responseText);
                        console.log(resData.key);
                        var remoteImg = config.imgUrlDomain + resData.key;
                        resolve(remoteImg);
                    }
                };
            }
        };
        xhr.send();
    };

    //兼容CommonJs规范
    if (typeof module !== "undefined" && module.exports) {
        module.exports = uploadQN;
    }
    //兼容AMD/CMD规范
    if (typeof define === "function")
        define(function () {
            return uploadQN;
        });
    //注册全局变量,兼容直接使用script标签引入插件
    global.uploadQN = uploadQN;
})(window);
//调用方法
uploadQN(imgdata, (res) => {
    //do something
});

特点:

  • 即插即用
  • 不支持配置
插件2.0
;(function (global) {
    //定义上传方法
    function UploadQN(img, options) {
        var config = {
            getTokenUrl: "//xxx.xxx.com/api/Active/qintoken?bucket=wnlother", //获取token接口地址
            qiniuUpUrl: "//xxx.qiniup.com/putb64/-1", //上传的七牛服务器地址
            imgUrlDomain: 'https://xxx.xxx.com/', // 图片域名
            success: function(res) {}
        };
        if (!(this instanceof UploadQN)) {
            console.log(0);
            return new UploadQN(img, options);
        }
        // options = options || config;
        // 合并参数
        for (var k in options) {
            if (options.hasOwnProperty (k)) {
                config [k] = options [k];
            }
        }
        console.log(config);
        // 1.获取token
        var xhr = new XMLHttpRequest();
        xhr.open('GET', config.getTokenUrl, true);
        xhr.onreadystatechange = function() {
            // readyState == 4说明请求已完成
            if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) {
                // 从服务器获得数据 
                var res = JSON.parse(xhr.responseText);
                // 2.上传七牛
                var xhr2 = new XMLHttpRequest();
                xhr2.open('POST', config.qiniuUpUrl, true);
                xhr2.setRequestHeader('Content-Type', 'application/octet-stream');
                xhr2.setRequestHeader('Authorization', 'UpToken ' + res.token);
                xhr2.send(img.substring(23));
                xhr2.onreadystatechange = function() {
                    if (xhr2.readyState === 4) {
                        var resData = JSON.parse(xhr2.responseText);
                        console.log(resData.key);
                        var remoteImg = config.imgUrlDomain + resData.key;
                        // 3.执行回调
                        config.success && config.success(remoteImg);
                    }
                };
            }
        };
        xhr.send();
    };

    //兼容CommonJs规范
    if (typeof module !== "undefined" && module.exports) {
        module.exports = UploadQN;
    }
    //兼容AMD/CMD规范
    if (typeof define === "function")
        define(function () {
            return UploadQN;
        });
    //注册全局变量,兼容直接使用script标签引入插件
    global.UploadQN = UploadQN;
})(window);
//调用方法
UploadQN(imgdata, {
    imgUrlDomain: 'https://other.image.cq-wnl.com/',
    success: (res) => {
       console.log(res);
    }
});
或
var loader = new UploadQN(imgdata, {
    imgUrlDomain: 'https://other.image.cq-wnl.com/',
    success: (res) => {
       console.log(res);
    }
});

特点:

  • 支持实例化
  • 支持配置
  • 没有api,不支持链式调用
插件3.0
/* eslint-disable */
;(function (global) {
    //定义上传方法
    function UploadQN(img, options) {
        this.img = img;
        this.config = {
            getTokenUrl: "//xxx.xxx.com/api/Active/qintoken?bucket=wnlother", //获取token接口地址
            qiniuUpUrl: "//xxx.qiniup.com/putb64/-1", //上传的七牛服务器地址
            imgUrlDomain: 'https://xxx.xxx.com/' // 图片域名
        };
        this.resultImg = '';
        if (!(this instanceof UploadQN)) {
            console.log(0);
            return new UploadQN(img, options);
        }
        // options = options || this.config;
        // 合并参数
        for (var k in options) {
            if (options.hasOwnProperty (k)) {
                this.config [k] = options [k];
            }
        }
        console.log(this.config);
        this.init();
    };
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值