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. Great StackOverflow questions

    1. diffenece between MVC and MVP http://stackoverflow.com/questions/2056/what-are-mvp-and-mvc-and-wh ...

  2. Linux中安装和使用rz、sz命令

    安装命令: yum install lrzsz 从服务端发送文件到客户端: sz filename 从客户端上传文件到服务端: rz在弹出的框中选择文件,上传文件的用户和组是当前登录的用户 Secur ...

  3. MATLAB - 练习程序,求灰度图像均值、最大、最小数值

    clear all; close all; clc img=imread('lena.bmp'); figure; imshow(uint8(img)); [m n]=size(img); img_m ...

  4. Android广播错误.MainActivity$MyReceiver; no empty constructor

    广播的定义,如果是内部类,必须为静态类. 下面总结一下作为内部类的广播接收者在注册的时候需要注意的地方:   1.清单文件注册广播接收者时,广播接收者的名字格式需要注意.因为是内部类,所以需要在内部类 ...

  5. Trie树:POJ2001

    这是一道最简单的trie树的题 有趣的是这道题的测试用例无法在VS上调试,只能在框框里不断提交测试了,哈哈 最基本的Trie树,插入和查找操作没什么好说的 注意节点添加一个count变量作为附加条件, ...

  6. OSIC Performance Bot

    https://github.com/lbragstad/keystone-performance

  7. android4.3 Bluetooth(le)分析之startLeScan分析

    BluetoothAdapter.java中有low enery(le)的一些方法,android提供了这些方法,但源码中并未找到这些方法的调用之处.本文档主要分析这类方法的执行流程,来了解下le到底 ...

  8. mysql mha 主从自动切换 高可用

    mha(Master High Availability)目前在MySQL多服务器(超过二台),高可用方面是一个相对成熟的解决方案. 一,什么是mha,有什么特性 1. 主服务器的自动监控和故障转移 ...

  9. 12.我们不是在真空里谈软件工程, 软件要运行在硬件芯片上面, 下面看看一个计算机芯片的发展历史: http://perspectives.mvdirona.com/2014/09/august-21-2014-computer-history-museum-presentation/ http://mvdirona.com/jrh/TalksAndPapers/DileepBhandar

    电脑芯片的诞生和发展是20世纪最伟大的发明之一,芯片技术决定了计算机升级换代的速度,决定了计算机小型化实现的程度,决定了计算机智能化的程度,决定了计算机普及化的应用深度. 1971年11月15日,英特 ...

  10. JavaScript 高性能笔记

    浏览器解析 JavaScript .CSS .DOM 时,一般都是单线程解析,所以,引用外部文件时的位置不同,UE体验也不同. 下面是 Yahoo 大牛 Nicholas C. Zakas 的 < ...