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()求周长
const PI = Math.PI; exports.area = (r) => PI * r * r; exports.circumference = (r) => 2 * PI * r;
调用这个Module app.js:
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类
'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:
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
Id='liubei' , Name='刘备'
Id='guanyu' , Name='关羽'
Id='zhangfei' , Name='张飞'
定义一个Module表示全局变量
user.js:使用Count()方法来统计实例化总数
'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
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
Id='liubei' , Name='刘备'
Id='guanyu' , Name='关羽'
Id='zhangfei' , Name='张飞'
count is 3
Nodejs in Visual Studio Code 06.新建Module的更多相关文章
- crossplatform---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 09.企业网与CNPM
1.开始 CNPM : https://npm.taobao.org/ 2.企业网HTTP代理上网 平时办公在一个大企业网(10.*.*.*)中,使用HTTP代理上网,发现npm命令无法执行. 解决方 ...
随机推荐
- linux意外关机,如何修复
意外关机后,提示an error occurred during the file system check. 解决方法,输入root密码 执行 fdisk -l 查看磁盘 (Repair files ...
- (转)Smarty Foreach 使用说明
foreach 是除 section 之外处理循环的另一种方案(根据不同需要选择不同的方案). foreach 用于处理简单数组(数组中的元素的类型一致),它的格式比 section 简单许多,缺点是 ...
- c# try..... catch
功能说明:在此例中,try 块包含对可能导致异常的ProcessString()方法的调用.catch子句包含仅在屏幕上显示消息的异常处理程序,当从ProcessString内部调用throw语句时, ...
- C# 数字证书微信API调用使用参考事例
X.509 v.3 证书的方法.一个比较完整的调用 微信 API的示例: private stringGetResponseResult() { string strRespons ...
- 转: Oracle AWR 报告 每天自动生成并发送邮箱
原贴地址:http://www.cnblogs.com/vigarbuaa/archive/2012/09/05/2671794.html Oracle AWR 介绍http://blog.csdn. ...
- ubuntu12.04安装QQ2013
1.下载Longene QQ2013SP6 http://pan.baidu.com/s/1hq83fWo 2.安装 1)如果之前安装过旧版本需要先卸载(通过dpkg -l | grep qq查看). ...
- (JAVA)从零开始之--对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- 371. Sum of Two Integers -- Avota
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...
- 4 - 执行TestNG
TestNG以如下几种方式被调用 命令行 ant Eclipse IntelliJ's IDEA 这部分对如何使用命令行方式调用TestNG进行阐述. 假设TestNG已经在你的classpath中, ...
- 用css3实现鼠标移进去当前亮其他变灰
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...