My first plugin

plugin插件↓↓

;(function (underfined) {
    "use strict"
    var _global;
    //插件函数
    var plugin = {
        add: function (n1, n2) {
            return n1 + n2;
        },
        sub: function (n1, n2) {
            return n1 - n2;
        },
        mul: function (n1, n2) {
            return n1 * n2;
        }
    }

    //最后将插件函数暴露给全局
    _global = (function () {
        return this || (0, eval)('this');
    }());
    if (typeof module !== 'undefined' && module.exports) {
        module.exports = plugin;
    } else if (typeof define === 'function' && define.amd) {
        define(function () {
            return plugin;
        });
    } else {
        !('plugin' in _global) && (_global.plugin = plugin);
    }

}());

  

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="plugin.js"></script>
</head>
<body>
<script>
    //引入插件,尽情使用plugin对象吧。。。。。。
    with(plugin){
        document.write(add(2,1));// 3
        document.write(mul(7,7));//49
    }
</script>
</body>
</html>

  

  

猜你喜欢

转载自www.cnblogs.com/Longhua-0/p/9278979.html