crossplatform---Nodejs in Visual Studio Code 06.新建Module
1.开始
Node.js:https://nodejs.org
2.Moudle
js编程中,由于大家可以直接在全局作用域中编写代码,使开发人员可以很容易的新建一个全局变量或这全局模块,这些全局变量或全局模块在工程化的开发中,极易互相冲突,同时也很难搞清楚它们之间互相的依赖关系。Node.js采用CommonJS规范来定义模块与使用模块,提供了required和module.exports来解决这个问题。
required()方法,通过required方法将其他模块引用到当前作用域内。
module.exports方法,从当前作用域中导出对象Object或类定义Function。
定义一个Module,仅包含静态方法
circle.js:提供了两个方法
area() 求面积,circumference()求周长
|
1
2
3
4
5
|
const PI = Math.PI;exports.area = (r) => PI * r * r;exports.circumference = (r) => 2 * PI * r; |
调用这个Module app.js:
|
1
2
|
const circle = require('./circle.js');console.log( `The area of a circle of radius 4 is ${circle.area(4)}`); |
上面的示例代码定义一个Module=.Net中包含静态方法的静态类。
定义一个Module,表示普通类
user.js:定义一个User类
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
'use strict';const util = require('util');function User(sId, sName) { this.Id = sId; this.Name = sName;}User.prototype.toString = function () { return util.format("Id='%s' , Name='%s'", this.Id, this.Name);}module.exports = User; |
app.js:
|
1
2
3
4
5
6
7
8
9
10
11
|
var User = require('./user');var pSource = [];pSource.push(new User('liubei','刘备'));pSource.push(new User('guanyu','关羽'));pSource.push(new User('zhangfei','张飞'));for (var index = 0; index < pSource.length; index++) { var element = pSource[index]; console.log( `${element.toString()}`);} |
console
|
1
2
3
|
Id='liubei' , Name='刘备'Id='guanyu' , Name='关羽'Id='zhangfei' , Name='张飞' |
定义一个Module表示全局变量
user.js:使用Count()方法来统计实例化总数
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
'use strict';const util = require('util');var nCount = 0;function User(sId, sName) { this.Id = sId; this.Name = sName; nCount++;}User.prototype.toString = function () { return util.format("Id='%s' , Name='%s'", this.Id, this.Name);}module.exports = User;module.exports.Count = function () { return nCount;} |
app.js
|
1
2
3
4
5
6
7
8
9
10
11
|
var User = require('./user');var pSource = [];pSource.push(new User('liubei','刘备'));pSource.push(new User('guanyu','关羽'));pSource.push(new User('zhangfei','张飞'));pSource.forEach(function(pUser) { console.log( `${pUser.toString()}`);}, this);console.log( `count is ${User.Count()}`); |
console
|
1
2
3
4
|
Id='liubei' , Name='刘备'Id='guanyu' , Name='关羽'Id='zhangfei' , Name='张飞'count is 3 |
http://www.cnblogs.com/mengkzhaoyun/p/5393784.html
crossplatform---Nodejs in Visual Studio Code 06.新建Module的更多相关文章
- Nodejs in Visual Studio Code 06.新建Module
1.开始 Node.js:https://nodejs.org 2.Moudle js编程中,由于大家可以直接在全局作用域中编写代码,使开发人员可以很容易的新建一个全局变量或这全局模块,这些全局变量或 ...
- Nodejs in Visual Studio Code 10.IISNode
1.开始 Nodejs in Visual Studio Code 08.IIS : http://www.cnblogs.com/mengkzhaoyun/p/5410185.html 参考此篇内容 ...
- Nodejs in Visual Studio Code 14.IISNode与IIS7.x
1.开始 部署IISNode环境请参考:Nodejs in Visual Studio Code 08.IIS 部署Nodejs程序请参考:Nodejs in Visual Studio Code 1 ...
- Nodejs in Visual Studio Code 11.前端工程优化
1.开始 随着互联网技术的发展,企业应用里到处都是B/S设计,我有幸经历了很多项目有Asp.Net的,有Html/js的,有Silverlight的,有Flex的.很遗憾这些项目很少关注前端优化的问题 ...
- Nodejs in Visual Studio Code 04.Swig模版
1.开始 设置Node_Global:npm config set prefix "C:\Program Files\nodejs" Express组件:npm install e ...
- Nodejs in Visual Studio Code 01.简单介绍Nodejs
1.开始 作者自己:开发人员,Asp.Net , html / js , restful , memcached , oracle ,windows , iis 目标读者:供自己以后回顾 2.我看No ...
- Nodejs in Visual Studio Code 07.学习Oracle
1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...
- Nodejs in Visual Studio Code 05.Swig+Bootstrap
1. 开始 准备好Express+Swig的练习代码:https://github.com/Mengkzhaoyun/nodepractise 准备好AdminLTE后台管理模版:https://ww ...
- Nodejs in Visual Studio Code 02.学习Nodejs
1.开始 源码下载:https://github.com/sayar/NodeMVA 在线视频:https://mva.microsoft.com/en-US/training-courses/usi ...
随机推荐
- Android-Junit-Report测试报告生成——Android自动化测试学习历程
视频地址: http://www.chuanke.com/v1983382-135467-384869.html 这个内容其实已经在用了,我在上一篇文章robotium—只有apk文件的测试中已经讲过 ...
- EF联合查询的新用法
用EF很多年了,做联合查询时,只知道linq和lambda两种语法,今天朋友发了一个链接,打开看后发现是EF内置的新的关于联合查询的方法,赶紧抄录下来,以备后用. 现在先把这几种方法,各写一个例子,便 ...
- DB2不记录事务日志
1. DB2大数据处理不记录事务日志步骤: 建表需要添加属性“NOT LOGGED INITIALLY” 在大批量更改操作的同一个事务开始时执行:“ALTER TABLE tabname ACTI ...
- 基于Jquery-ui的自动补全
1.添加CSS和JS引用 <script type="text/javascript" src="javascript/jquery-1.7.min.js" ...
- SUSE Linux Enterprise Server 11 软件源
1.添加软件源 zypper ar http://ftp5.gwdg.de/pub/opensuse/discontinued/distribution/11.4/repo/oss oss zyppe ...
- Spring 4 官方文档学习(十一)Web MVC 框架之约定优于配置
当返回一个ModelAndView时,可以使用其addObject(Object obj)方法,此时的约定是: An x.y.User instance added will have the nam ...
- DIV布局-高度不同DIV,自动换行并对齐
最近弄了一个动态添加div框,每个div框内容有多有少,要支持div高度自适应,还要添加的div自动追加,并且换行还要保持每行对齐. 刚开始的效果: 要改啊,搞不定,问了UI高手,终于给出了完美解决方 ...
- Java泛型总结(转)
本文是转载,原文链接:http://www.cnblogs.com/lwbqqyumidi/p/3837629.html 一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: ...
- nginx 版本介绍
Nginx官网提供了三个类型的版本Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版Stable version:最新稳定版,生产环境上建议使用的版 ...
- mysql之group_concat函数详解
函数语法: group_concat( [DISTINCT] 要连接的字段 [Order BY 排序字段 ASC/DESC] [Separator '分隔符'] ) 下面举例说明: sele ...