当前位置:网站首页>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 :
边栏推荐
猜你喜欢

Tropomi (sentinel 5p) data introduction and download method

【着色器实现RoundWave圆形波纹效果_Shader效果第六篇】
![[100 cases of scratch drawing] Figure 46-scratch drawing flowers children's programming scratch programming drawing case tutorial grade examination competition drawing training case](/img/44/832686f3ee198772794cbd53e7d5a5.png)
[100 cases of scratch drawing] Figure 46-scratch drawing flowers children's programming scratch programming drawing case tutorial grade examination competition drawing training case

The third slam Technology Forum - Professor wuyihong

信号的理解

Jetson nano烧录踩坑记(一定可以解决你的问题)

支付宝常用接口统一封装,可直接支付参数使用(适用于H5、PC、APP)

LU_ Asr01 voice module usage

高数下|二重积分的计算4|高数叔|手写笔记

prime_series_level-1
随机推荐
【isprint函数判断字符是否可输出】
Green Tao theorem (4): energy increment method
UnauthorizedAccessException:Access to the path “/xx/xx.xx“ is denied
视觉slam学习|基础篇01
Proof of green Tao theorem (1): preparation, notation and Gowers norm
支付宝常用接口统一封装,可直接支付参数使用(适用于H5、PC、APP)
模块化开发
【Scratch画图100例】图46-scratch绘制花朵 少儿编程 scratch编程画图案例教程 考级比赛画图集训案例
[kernel] platform bus model for driving development and learning
三层交换机配置MSTP协议详解【华为eNSP实验】
高数下|二重积分的计算2|高数叔|手写笔记
确定括号序列中的一些位置
Tropomi (sentinel 5p) data introduction and download method
From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
OOM机制
The common interfaces of Alipay are uniformly encapsulated and can be used directly for payment parameters (applicable to H5, PC, APP)
(Note)优化器Adam的学习率设置
Today's sleep quality record 81 points
[100 cases of scratch drawing] Figure 46-scratch drawing flowers children's programming scratch programming drawing case tutorial grade examination competition drawing training case
Car rental vehicle management system based on jsp+ssm+mysql car rental
(1) Inside anonymous functions , Define an object .