Home  >  Article  >  Web Front-end  >  Nodejs modularization: nodejs calculation permutations and combinations (code)

Nodejs modularization: nodejs calculation permutations and combinations (code)

不言
不言Original
2018-08-15 16:23:462782browse

The content of this article is about nodejs modularization: nodejs calculation permutation and combination (code), which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Using nodejs to calculate permutations and combinations is an example:

Permutations and combinations require factorial, so create new main.js and jiecheng.js files and pailiezuhe.jsFile (because of learning modularization, so create two files)

Copy the following code into jiecheng.js, comment in detail

var abc = 100;

// 计算阶乘的方法
function jiecheng(n){
    var con = 1;
    for(n;n>0;n--){
        con = con*n;
    } 
    return con;
}

// module.exports 表示本js文件所导出的内容,默认是一个空对象
// 通过给modul.exports赋值可以设置本js文件所导出的内容
module.exports = jiecheng;

The last one above is the export The current factorial method

So when calling pailiezuhe.js, the factorial method is first imported

pailiezuhe.js code:

console.log(123);

// 使用require可以在一个js文件中导入另一个js文件
// 参数表示要导入的js文件,内容是要导入的js文件的路径(可以是相对路径也可以是绝对路径)
// 返回值是导入的js文件中所导出的内容

//  ./表示本js文件所在的目录
var jc = require("./jiecheng.js");


// 使用require导入一个js文件仅仅是将这个js文件导出的内容导入,然后赋值给一个变量,并不会导入这个js文件中的其他内容,也就是说这两个js文件的作用域是隔离的
// console.log(abc);

function pailie(n,m){
    return jc(n)/jc(n-m);
}

function zuhe(n,m){
    return jc(n)/(jc(m)*jc(n-m));
}
// 使用module.exports只能导出一个内容,如果需要导出多个内容,可以把这些内容封装成一个对象,然后导出这个对象
// module.exports = {
//     pailie:pailie,
//     zuhe:zuhe
// };
// 或者是给exports添加属性也是一样
module.exports.pailie = pailie;
module.exports.zuhe = zuhe;

main.js code

// 当代码量很大时,可以将代码分散在多个js文件中,每个js文件单独实现一个小功能,这些js文件共同组成一个完整的大功能

// 在浏览器环境中,可以通过多个script标签导入多个js文件.但是这种合并代码的方法有一下缺点:
// 1,多个js文件的导入必须按照依赖关系先后导入
// 2,使用script标签导入的多个文件本质上是拼接成了一个js文件,所以这些js文件运行时都处于同一个全局作用域,那么这些js文件中不能使用同名的全局变量.


// -------------------------------------------------------------------------------

// 在nodejs中也可以实现将不同的功能写入不同的js文件,在某个js文件中需要什么功能就导入哪个功能的js文件
// 这叫做nodejs的模块化

// 当 第一次 导入某个js文件时,这个js文件会执行并获得导出内容,如pailiezuhe.js里console.log()会执行
var plzh = require("./pailiezuhe.js");

// 某个js文件运行得到导出内容之后,导出的内容会被缓存起来,下次再导入这个js文件时,就会直接获得上次的导出结果,不再运行js文件,例如下面这两行如果不注释也不会执行pailiezuhe.js的console
// var q = require("./pailiezuhe.js");
// var w = require("./pailiezuhe.js");

var n = 10;
var m = 4;
console.log(plzh.pailie(n,m));
console.log(plzh.zuhe(n,m));

Related Recommendation:

Easily create nodejs server (3): Code modularization_node.js

nodejs require module (file module/core Module) and path introduction_Basic knowledge

The above is the detailed content of Nodejs modularization: nodejs calculation permutations and combinations (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn