zoukankan      html  css  js  c++  java
  • 在浏览器环境使用js库(不用require功能)

    在小程序中使用 js 库时会要用到 require/import/export 等功能或指令(ES6)。而当在浏览器环境下使用这些库时,由于require等使用起来比较麻烦。此时可对库源码稍作改动,采用 return {name1, name2} 方式即可。代码框架如下所述。

    1、 采用 require/import/export
    // index.js
    import tools from "./tools.js";
    var lca = tools.add();
    // tools.js
    function add(){};
    function mul(){};
    module.exports = {add:add, mul:mul,};
    2、不采用 require/import/export
    // index.html
    // <script src="./tools.js"></script>
    const tools = tools_proc();
    var lca = tools.add();
    // tools.js
    function tools_proc(){  //最外层包裹一个函数
       function add(){};
       function mul(){};
       // module.exports = {add:add, mul:mul,};
       return {add:add, mul:mul};
    }

     代码测试环境:Firefox v84, Chrome v87,Edge v87, IE 11。

  • 相关阅读:
    Valid Anagram
    Spiral Matrix II
    Spiral Matrix
    Kth Smallest Element in a BST
    Count Primes
    javascript 判断浏览器
    javascript 数值交换技巧
    EntityFramework 6 分页模式
    JSON.parse 和 JSON.stringify
    CSS z-index
  • 原文地址:https://www.cnblogs.com/xyyztx/p/14176348.html
Copyright © 2011-2022 走看看