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

  1. crossplatform---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 09.企业网与CNPM

    1.开始 CNPM : https://npm.taobao.org/ 2.企业网HTTP代理上网 平时办公在一个大企业网(10.*.*.*)中,使用HTTP代理上网,发现npm命令无法执行. 解决方 ...

随机推荐

  1. 很好用的Tab标签切换功能,延迟Tab切换。

    一个网页,Tab标签的切换是常见的功能,但我发现很少有前端工程师在做该功能的时候,会为用户多想想,如果你觉得鼠标hover到标签上,然后切换到相应的内容,就那么简单的话,你将是一个不合格的前端工程师啊 ...

  2. PTHREAD_MUTEX_INITIALIZER问题

      PTHREAD_MUTEX_INITIALIZER 与 expected expression before ‘{’ token 在进行 Posix thread 编程时,出现以下编译错误:err ...

  3. .Net framework.

    Figure 1 - .Net Framework The Common Language Runtime (CLR) is the mechanism through which .NET code ...

  4. Typescript 团队合作的利器

    前言 在介绍Typescript 之前,我需要隆重介绍一个人: 安德斯·海尔斯伯格(Anders Hejlsberg,1960.12~),丹麦人,Turbo Pascal编译器的主要作者,Delphi ...

  5. Objective-C和C++的区别

    1.都是有C语言延伸而来2.OC是完全动态的,C++是部分动态的3.OC不支持多继承,通过代理 类别 协议优雅的实现了相关的一系列特性4.调用机制不同OC里面叫发送消息  C++叫做调用函数数5.OC ...

  6. 十六、C# 常用集合类及构建自定义集合(使用迭代器)

    常用集合类及构建自定义集合 1.更多集合接口:IList<T>.IDictionary<TKey,TValue>.IComparable<T>.ICollectio ...

  7. 2016.7.16equals的使用(一)

    class V{ private int a; V(int a){ rhis a=a; } public  boolean equals(int a,int b){ if(this.a equals( ...

  8. SGU 158.Commuter Train

    一道简单题. 火车停的位置不是在整点就是在二分之一点,坐标*2,然后枚举火车停的位置,计算总距离即可. code: #include <iostream> #include <cma ...

  9. 第三篇、调优之路 Apache调优

    1.  简介 在第一篇中整合了apache + tomcat ,利用了apache解析静态文件为tomcat解压.但是在测试机上发现两者性能不足,不能充分利用服务器的性能,该篇中将对apache进行性 ...

  10. iOS 相机手动对焦

    AVCaptureDevice的方法,之前查了网上和stackoverflow上,没有,于是自己试着做了下,成功了,分享下. //实例化 AVCaptureDevice *captureDevice ...