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 ...
随机推荐
- SegmentFault创始人高阳:辍学后带着500元北漂,4年建成国内最大开发者
i黑马注:i黑马曾经和高阳聊过几次天,在他身上我看到了90后CEO特别明显的成功特质“敢为天下先”.在别人犹豫的时候敢第一个出手,在互联网时代往往会取得最关键的“先机优势”. 7月19日,“腾讯产品家 ...
- POJ 1236-Network of Schools (图论-有向图强联通tarjan)
题目链接:http://poj.org/problem?id=1236 题目大意:N(2<N<100)个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输.问题 ...
- PHP验证码参考页面
http://blog.sina.com.cn/s/blog_95ee14340100z8q9.html http://www.jb51.net/article/44951.htm
- NGUI Draw Calls优化(思路)
用NGUI做界面的时候发现不注意GameObject(或者说Widget)的depth的话,单独运行界面时,Draw Calls挺高的: 网上搜了一下,大把的博客说的都是类似以下的原则: (PS:以下 ...
- mysql查询数据返回touple改为字典的方法
conn = MySQLdb.connect(host='ip',user='root',passwd='123456',db="dbname",charset="utf ...
- hdu 4006 The kth great number (优先队列)
/********************************************************** 题目: The kth great number(HDU 4006) 链接: h ...
- hibernate进行多表联合查询
hibernate是按照hql语句来进行查询的, 里面所使用的表名, 其实是实体类的名字, hql语句的写法并没有多大差别, 是在返回结果的时候要稍微做一些处理 //使用hibernate进行多表查询 ...
- 小甲鱼python视频弟十一讲(课后习题)
1.修改列表里的值 list1 = [,,[,,,[,,,,] list1[] = print(list1) list1[][][] = '?' print(list1) 2.列表的排序(sort) ...
- 【Lua】Debian环境下openresty的安装
OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器,它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项. OpenResty 通 ...
- 钩子机制(hook)
钩子是编程惯用的一种手法,用来解决一种或多种特殊情况的处理. 简单来说,钩子就是适配器原理,或者说是表驱动原理,我们预先定义了一些钩子,在正常的代码逻辑中使用钩子去适配一些特殊的属性,样式或事件,这样 ...