ES6之导入模块时的内存共享

时间:2023-03-09 20:25:45
ES6之导入模块时的内存共享

项目结构

ES6之导入模块时的内存共享

主页面

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>测试</title>
<script type="module" src="index.js"></script>
</head>
<body> </body>
</html>

index.js脚本

import {cruiser} from './toyato.js';
import {discovery} from './land-rover.js';
import {tire} from './tire.js'; console.log(`轮胎使用厂家数量: ${tire.count}`);

toyato.js

import {tire} from './tire.js';

tire.count++;

export let cruiser = {
name : 'Land Cruiser'
};

land-rover.js

import {tire} from './tire.js';

tire.count++;

export let discovery = {
name : 'Land Rover Discovery'
};

tire.js

// 导出一个轮胎对象

export let tire = {
name : 'BRIDGESTONE',
count : 0
};

运行结果

ES6之导入模块时的内存共享