巨大的石头

在这里寻找精彩人生

导航

javascript import export 简单示例

概述

import是ES6引入的新特性,它允许你以声明式的方式导入其他模块中的内容。require是Node.js中的特性,它允许你使用一个函数来加载和导入其他模块。

使用方法

utils.js

export const add=(a,b)=>a+b;
export const subtract=(a,b)=>a-b;

 

index.js

import * as utils from './utils.js';
console.log(utils.add(1,20));

  

index.html

<html>
    <head>
        <script type="module" src="./index.js"></script>
    </head>
    <body>

    </body>
</html>

 

执行结果

执行结果:21

 

posted on 2024-02-14 18:37  巨大的石头  阅读(17)  评论(0编辑  收藏  举报

打赏