当前位置:网站首页>Modular development
Modular development
2022-07-23 21:02:00 【_ Semantic error】
Catalog
One 、 Why modular development is needed ?( understand )
1、 Modularity has two cores : Export and import
One 、 Why modular development is needed ?( understand )
- Early stage of Web Development ,js Do some Simple form verification or animation implementation , Less code . Write the code directly in <script> In the label ;
- ajax The emergence of asynchronous requests , Slowly form front and rear end separation , More and more things need to be done by the client , The amount of code is also increasing . In response to the surge in code , We usually Organize code in multiple js In file , For maintenance . But this way of maintenance , Still can't avoid some Catastrophic problems : such as Global variable homonymy problem , in addition , The way this code is written Yes js The dependency order of files is almost mandatory ; But when js Too many documents , For example, when there are dozens of them , It's a relatively simultaneous thing to figure out their order .
- We can use Anonymous functions to solve the problem of duplicate names , But if we want to be in main.js In file , be used aaa.js Defined flag, How to deal with it ? obviously , Another file is not easy to use , because flag Is a local variable .
- Use the module as an exit : We can use Variables that will need to be exposed , Use a module as an exit , Look at the corresponding code :
(1) Inside anonymous functions , Define an object .
(2) To object Add various properties and methods that need to be exposed ( No direct definition of exposure is required ).
(3) Finally, return the object to , And used one outside MoudleA Accept .
Next , We are man.js How to use it in ?
We just need to use the properties and methods of our own module
This is the most basic encapsulation of modules , In fact, there are many advanced topics about module encapsulation :
But we Here is to understand why modules are needed , And the original prototype of the module .
Fortunately, , Front end modular development has many existing specifications , And the corresponding implementation scheme .
Common modular specifications :CommonJS、AMD、CMD, Also have ES6 Of Modules
Two 、CommonJS
1、 Modularity has two cores : Export and import
CommonJS Export of :
module.export = {
flag:true,
test(a,b){
return a + b
},
demo(a,b){
return a * b
}
}CommonJS Import of :
let {test,demo,flag} = require('modeleA');
// Equate to
let _mA = require('moduleA');
let test = _mA.test;
let demo = _mA.demo;
let flag = _mA.flag;
2、ES6 Of export Basic use :
export Instructions for Export variables ,2 Methods :
// Method 1
export let name = 'Hi'
export let age = 18
// Method 2
let name = 'Hi'
let age = 18
export{name,age}
Output function or output class ,2 Methods :
// Method 1
export function test(content){
console.log(content);
}
export class Person {
constructor(name,age){
this.name = name;
this.age = age;
}
// Defined function
run(){
console.log(this.name + ' Running ');
}
}// Method 2
function test(content){
console.log(content);
}
export class Person {
constructor(name,age){
this.name = name;
this.age = age;
}
// Defined function
run(){
console.log(this.name + ' Running ');
}
}
export{test.Person}
export default
Use scenario : A module contains a function , We don't want to name this feature , And let the importers name themselves
// info.js
export default function (){
console.log('default function');
}
// main.js
import myFunc from './info.js'
myFunc()
Be careful :export default In the same module , It is not allowed to have more than one .
3、ES6 Of import Use
Use export The interface provided by the instruction export module , Now we can go through the import Command to load the corresponding module
First , We need to be in HTML The code introduces two js file , And the type needs to be set to module
import Instruction is used to import the contents of the module , such as main.js Code for
import {name,age,height} from "./info.js"
console.log(name,age,height);
If we want to All information in a module is imported , Import one by one is obviously troublesome :
adopt * You can import all the export Variable
But usually we need to give * Make up an alias , Convenient for subsequent use
import * as info from './info.js'
console.log(info.name,info.age,info.height);
Reference resources :
边栏推荐
- Car rental vehicle management system based on jsp+ssm+mysql car rental
- 第三届SLAM技术论坛-吴毅红教授
- The third slam Technology Forum - Professor wuyihong
- MySQL(3)
- [kernel] platform bus model for driving development and learning
- Leetcode hot topic hot52-100
- LU_ Asr01 voice module usage
- Cesium 核心类Viewer-查看器详解
- [Yunxiang book club No. 13] Chapter V ffmpeg common methods for viewing media information and processing audio and video files
- Cesium 键盘鼠标控制相机漫游(源码+原理讲解)
猜你喜欢

prime_series_level-1
![[shader realizes roundwave circular ripple effect _shader effect Chapter 6]](/img/3f/90c2f0004303dc577eba1615fa3fd7.png)
[shader realizes roundwave circular ripple effect _shader effect Chapter 6]

实现生成订单30分钟未支付,则自动取消

TypeScript基础

Preprocessing tropomi (sentinel 5p) data with envi

Improving Performance with Explicit Rendering(通过显式渲染提高性能)

【复数 重载运算符】

Day 12: continued day 11 (BGP related knowledge)

WinDbg实践--入门篇

Jetson nano recording stepping on the pit (it will definitely solve your problem)
随机推荐
TROPOMI(哨兵5P)数据介绍及下载方法
OpenLayers实例-Advanced View Positioning-高级视图定位
【创建 Birthday Card 应用】
《迷失》stray工人帽子获得方法 工人安全帽在哪里?
1062 Talent and Virtue
三层交换机配置MSTP协议详解【华为eNSP实验】
第3章业务功能开发(创建线索)
2022.7.11mySQL作业
Preprocessing tropomi (sentinel 5p) data with envi
VLAN综合实验
Jetson nano recording stepping on the pit (it will definitely solve your problem)
模块化开发
支付产品及其使用场景
Green Tao theorem (3): anti uniform functions and their generated sigma Algebras
如何在面试中介绍自己的项目经验
LU_ASR01语音模块使用
高数下|三重积分的计算1|高数叔|手写笔记
Educational codeforces round 132 A-D problem solution
OpenLayers实例-Animated GIF-GIF动画
WinDbg practice -- Introduction
(1) Inside anonymous functions , Define an object .