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 ...
随机推荐
- pickle
[pickle] Python提供了pickle模块来实现序列化. 首先,我们尝试把一个对象序列化并写入文件: pickle.dumps()方法把任意对象序列化成一个bytes,然后,就可以把这个by ...
- 树莓派开发系列教程3——树莓派rasp-config配置(转)
经过前面两步我们的树莓派已经正常的工作起来了,但是在真正用它开发之前还需要进行一些列的配置以及软件的安装,这样开发起来才会得心应手,下面我们介绍一下常用的软件和服务 1.配置选项: 树莓派第一次使用的 ...
- AS错误:Error:Execution failed for task ':gM99SDK:processReleaseResources'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'F:\BaiduYunDown
原因,buildToolsVersion 版本太低. 在build.gradle文件设置 buildToolsVersion 设置高一点,但必须是SDK里面有的.
- Cable master poj1064(二分)
http://poj.org/problem?id=1064 题意:共有n段绳子,要求总共被分为k段.问在符合题意的前提下,每段长最大是多少? #include <iostream> #i ...
- Lamp 安装(CentOS6.6, php-5.4.39, httpd-2.4.12, mysql-5.6.24)
软件版本: pcre-8.36.tar.gzapr-1.5.1.tar.gzapr-util-1.5.4.tar.gzhttpd-2.4.12.tar.bz2 mysql-5.6.24.tar.gz ...
- three.js 源码注释(三十九)Light/HemisphereLight.js 半球光、 自然光(天光效果)
/*** * HemisphereLight类 是在场景中创建半球光,就是天光效果,经常用在室外,将各个位置的物体都照亮,室内的光线大多是方向性的, * 无论是窗口还是灯槽,用平面光很方便,室外用平面 ...
- C# web winform 路径
string path = ""; if (System.Environment.CurrentDirectory == AppDomain.Curr ...
- 关于loadrunner录制不跳转到IE
我是一个新手,对于这个问题,我已经愁了两周左右,因为是自学,一直没人教,靠自己百度也一直解决不了. 今天,我总算解决了这个问题. 我之前是ie8,根据网上说的启动IE----工具---Internet ...
- 在CentOS6.7操作系统上编译安装httpd2.4
功能描述: 在CentOS6.7操作系统上,编译安装apache服务,实现定制功能等 一.安装前提 1)安装编译httpd需要的软件包 [root@bqe6tewv41kx ~]# yum -y i ...
- Java中FileOutputStream和FileInputStream使用例子
package a.ab; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.F ...