js中插件的封装与调用

(function () {
    "use strict"
    let _global;

    function Add(data) {
        this.defaulData = {
            a: 0,
            b: 0,
        }
        this.data = Object.assign(this.defaulData, data)
        this.init()
    }

    Add.prototype = {
        init: function () {
            console.log(this.data)
        },
        add: function (...a) {
            console.log(a)
            let total = 0
            for (let i = 0; i < a.length; i++) {
                total += Number(a[i])
            }
            return total
        }
    }
    // 最后将插件对象暴露给全局对象
    _global = (function () {
        return this || (0, eval)('this');
    }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = Add;
    } else if (typeof define === "function" && define.amd) {
        define(function () {
            return Add;
        });
    } else {
        !('Add' in _global) && (_global.Add = Add);
    }
})()
let c = new Add({
    a:1,
    b:2
})
console.log(c.add(132,456,89,'a'))

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注