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. const PI = Math.PI;
  2.  
  3. exports.area = (r) => PI * r * r;
  4.  
  5. exports.circumference = (r) => 2 * PI * r;

调用这个Module app.js:

  1. const circle = require('./circle.js');
  2. console.log( `The area of a circle of radius 4 is ${circle.area(4)}`);

上面的示例代码定义一个Module=.Net中包含静态方法的静态类。

定义一个Module,表示普通类

user.js:定义一个User类

  1. 'use strict';
  2.  
  3. const util = require('util');
  4.  
  5. function User(sId, sName) {
  6. this.Id = sId;
  7. this.Name = sName;
  8. }
  9.  
  10. User.prototype.toString = function () {
  11. return util.format("Id='%s' , Name='%s'", this.Id, this.Name);
  12. }
  13.  
  14. module.exports = User;

app.js:

  1. var User = require('./user');
  2.  
  3. var pSource = [];
  4. pSource.push(new User('liubei','刘备'));
  5. pSource.push(new User('guanyu','关羽'));
  6. pSource.push(new User('zhangfei','张飞'));
  7.  
  8. for (var index = 0; index < pSource.length; index++) {
  9. var element = pSource[index];
  10. console.log( `${element.toString()}`);
  11. }

console

  1. Id='liubei' , Name='刘备'
  2. Id='guanyu' , Name='关羽'
  3. Id='zhangfei' , Name='张飞'

定义一个Module表示全局变量

user.js:使用Count()方法来统计实例化总数

  1. 'use strict';
  2.  
  3. const util = require('util');
  4.  
  5. var nCount = 0;
  6.  
  7. function User(sId, sName) {
  8. this.Id = sId;
  9. this.Name = sName;
  10. nCount++;
  11. }
  12.  
  13. User.prototype.toString = function () {
  14. return util.format("Id='%s' , Name='%s'", this.Id, this.Name);
  15. }
  16.  
  17. module.exports = User;
  18.  
  19. module.exports.Count = function () {
  20. return nCount;
  21. }

app.js

  1. var User = require('./user');
  2.  
  3. var pSource = [];
  4. pSource.push(new User('liubei','刘备'));
  5. pSource.push(new User('guanyu','关羽'));
  6. pSource.push(new User('zhangfei','张飞'));
  7.  
  8. pSource.forEach(function(pUser) {
  9. console.log( `${pUser.toString()}`);
  10. }, this);
  11. console.log( `count is ${User.Count()}`);

console

  1. Id='liubei' , Name='刘备'
  2. Id='guanyu' , Name='关羽'
  3. Id='zhangfei' , Name='张飞'
  4. 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. jetty服务器访问系统的域名

    jetty-env.xml=><Set name="contextPath">/epps-compensation-backend</Set> 这个决 ...

  2. java异常类的使用

    1.异常的概念 什么是异常?程序出错分为两部分,编译时出粗和运行时出错.编译时出错是编译器在编译源码时发生的错误: 运行时出错是在编译通过,在运行时出现的错误.这种情况叫异常. 例如:数组越界,除数为 ...

  3. angularjs-googleMap googleMap api地址解析与反解析

    1.js:根据地址得到经纬度var myplace=$scope.place;//获取输入的地址var geocoder = new google.maps.Geocoder();//创建geocod ...

  4. C#中区别多态、重载、重写的概念和语法结构

    C#中区别多态.重载.重写的概念和语法结构 重写是指重写基类的方法,在基类中的方法必须有修饰符virtual,而在子类的方法中必须指明override. 格式: 基类中: public virtual ...

  5. Angularjs总结(六) 上传附件

    所用插件:angular-file-upload 这个插件用到的几个指令:nv-file-select(点击选择).uploader(用于绑定控制器中新建的uploader对象) HTML: < ...

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

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

  7. 给表格设置border还可以这样玩

    <table width="100%" border="0" cellpadding="0" cellspacing="1& ...

  8. 再看IOC, 读深入理解DIP、IoC、DI以及IoC容器

    IoC则是一种 软件设计模式,它告诉你应该如何做,来解除相互依赖模块的耦合.控制反转(IoC),它为相互依赖的组件提供抽象,将依赖(低层模块)对象的获得交给第三方(系统)来控制,即依赖对象不在被依赖模 ...

  9. 源码来袭!!!基于jquery的ajax分页插件(demo+源码)

    前几天打开自己的博客园主页,无意间发现自己的园龄竟然有4年之久了.可是看自己的博客列表却是空空如也,其实之前也有写过,但是一直没发布(然而好像并没有什么卵用).刚开始学习编程时就接触到博客园,且在博客 ...

  10. WPF WebBrowser 不可见问题的解析[转]

    问题概述: 1.在Xaml中加入WebBrowser(不论是WPF中的控件,还是Winform中的控件) 2.设置Window Background="Transparent" A ...