当前位置:网站首页>Use between nodejs modules

Use between nodejs modules

2022-04-23 16:58:00 Endless cake

practice

Separately build A,B,C Three independent js modular .
A Module implementation summation
B Module implementation average
C Module reference B modular , At the same time, complete the process of first summing and then averaging

A modular

//eval()  Function to evaluate a string , And execute the  JavaScript  Code .
module.exports={	//	 Declare the module component first 
	sum(...arg){	// use ES6 Extension operator , Put all received parameters into a set 
	return eval(arg.join('+'))		// Connection string , And calculate 
	}

}

B modular :

// First introduce A modular 
let A = require('./a')		// Be sure to add.  ./  Suffixes can be omitted 

module.exports={	//	 Declare the module component first 
	avg(...arg){	//( here arg Will be C An array passed from the component )
	// Expand each item in an array through the expansion operator , Pass them on to sum, Then divide by the length of the array 
	return A.sum(...arg) / arg.length
	}

C modular :

let B = require('./b')		
console.log(B.avg(12,23,34,45,56,67,78,89))

Then execute directly through the command line C.js

 Insert picture description here

版权声明
本文为[Endless cake]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554520133.html