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的更多相关文章

  1. Nodejs in Visual Studio Code 06.新建Module

    1.开始 Node.js:https://nodejs.org 2.Moudle js编程中,由于大家可以直接在全局作用域中编写代码,使开发人员可以很容易的新建一个全局变量或这全局模块,这些全局变量或 ...

  2. Nodejs in Visual Studio Code 10.IISNode

    1.开始 Nodejs in Visual Studio Code 08.IIS : http://www.cnblogs.com/mengkzhaoyun/p/5410185.html 参考此篇内容 ...

  3. 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 ...

  4. Nodejs in Visual Studio Code 11.前端工程优化

    1.开始 随着互联网技术的发展,企业应用里到处都是B/S设计,我有幸经历了很多项目有Asp.Net的,有Html/js的,有Silverlight的,有Flex的.很遗憾这些项目很少关注前端优化的问题 ...

  5. Nodejs in Visual Studio Code 04.Swig模版

    1.开始 设置Node_Global:npm config set prefix "C:\Program Files\nodejs" Express组件:npm install e ...

  6. Nodejs in Visual Studio Code 01.简单介绍Nodejs

    1.开始 作者自己:开发人员,Asp.Net , html / js , restful , memcached , oracle ,windows , iis 目标读者:供自己以后回顾 2.我看No ...

  7. Nodejs in Visual Studio Code 07.学习Oracle

    1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...

  8. Nodejs in Visual Studio Code 05.Swig+Bootstrap

    1. 开始 准备好Express+Swig的练习代码:https://github.com/Mengkzhaoyun/nodepractise 准备好AdminLTE后台管理模版:https://ww ...

  9. Nodejs in Visual Studio Code 02.学习Nodejs

    1.开始 源码下载:https://github.com/sayar/NodeMVA 在线视频:https://mva.microsoft.com/en-US/training-courses/usi ...

随机推荐

  1. [转]Excel 取汉字拼音首位

    转自:http://jingyan.baidu.com/article/63acb44adca44461fcc17e85.html 转自:http://jingyan.baidu.com/articl ...

  2. JSON基本用法

    JSON基本用法 2016-08-10 16:42:19   JSON的全称是“JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基于文本,独立于语 ...

  3. 技术英文单词贴--R

    R redirect 重定向,改变方向 reference 参考,提及,引用 register 注册,登记,挂号 render 渲染 represent 代表,象征 route 路线,路由,通道 ro ...

  4. servlet实现的三种方式对比(servlet 和GenericServlet和HttpServlet)

    第一种: 实现Servlet 接口 第二种: 继承GenericServlet 第三种 继承HttpServlet (开发中使用) 通过查看api文档发现他们三个(servlet 和GenericSe ...

  5. MYSQL存储过程:批量更新数据2(产品品牌)

    执行语句 DELIMITER $$ DROP PROCEDURE IF EXISTS jsjh_goods_property_value_update$$ CREATE PROCEDURE jsjh_ ...

  6. 团队项目作业:利用NABCD模型进行竞争性需求分析

    NABC正是这样的一套框架,当你试图提出一项崭新的提案之际,它能够提供四个思维基点,令你的商业策划具备天马行空的基础. 具体来说,NABC是四个关键词的首字母缩写- Need(需求)-现在市场上未被满 ...

  7. HP 7440老机器重启

    一大早内存就报内存100% 处理流程 1.kmeminfo -u | more ,找出内存占用过大的进程ID --------------------------------------------- ...

  8. 模拟Linux的shell

    在学习了Linux的进程控制之后,学习了fork函数和exec函数族,通过这些个函数可以简单的实现一份shell,就是实现一份命令行解释器,当然是简单版的,实现功能如下 能执行普通的命令如ls ,ps ...

  9. Linux环境下MySQL报Table 'xxx' doesn't exist错误解决方法

    修改了lower_case_table_names=1 后,业务发有个库的表打不开了,看了表名以前是大写,查了一下如果设置不区分大小写, 以前的大小表名要改成小写.重启服务后可用! MYSQL在LIN ...

  10. 特征脸(Eigenface)理论基础-PCA(主成分分析法)

    在之前的博客  人脸识别经典算法一:特征脸方法(Eigenface)  里面介绍了特征脸方法的原理,但是并没有对它用到的理论基础PCA做介绍,现在做补充.请将这两篇博文结合起来阅读.以下内容大部分参考 ...