1、新建调用的js

第一种调用没有初始值的模块

var    http    =    require('http');
var User = require('./module/User');//引入的是user模块 不是user.js
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ //清除第2此访问
user = new User(); //实例化这个模块
user.id = 1;
user.name = "张三";
user.age = 20; user.enter(); response.end('');
}
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
function User(){
this.id;
this.name;
this.age;
this.enter = function(){
console.log('我叫'+this.name+',我的年龄是'+this.age);
}
}
module.exports = User;//导出这个模块/类,让其他js能引入

第二种调用初始值的模块

var    http    =    require('http');
var User = require('./module/User');//引入的是user模块 不是user.js
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ //清除第2此访问
user = new User(1,"zhangsan",20); //实例化这个模块 user.enter();//执行方法 response.end('');
}
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
function User(id,name,age){
this.id = id;
this.name = name;
this.age = age;
this.enter = function(){
console.log('我叫'+this.name+',我的年龄是'+this.age);
}
}
module.exports = User;//导出这个模块/类,让其他js能引入

2、一个模块继承另一个模块,然后调用

调用的js

var    http    =    require('http');

var Teacher = require('./module/Teacher'); 

http.createServer(function    (request,    response)    {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ //清除第2此访问 teacher = new Teacher(1,"zhangsan",20);
teacher.enter();//对父类的方法调用
teacher.teach(response);//调用自己的方法 response.end('');
}
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');

User类

function User(id,name,age){
this.id = id;
this.name = name;
this.age = age;
this.enter = function(){
console.log('我叫'+this.name+',我的年龄是'+this.age);
}
}
module.exports = User;//导出这个模块/类,让其他js能引入

Teacher类   并且继承User类

var User = reqire('./User');
function Teacher(id,name,age){
User.apply(this,[id,name,age]);//这句话实现了继承User这个模块,并且初始化,里面的id,name,age 就是Teacher(id,name,age)里面传进来的 this.teach = function(res){
res.write(this.name+"讲课");
} }
module.exports = Teacher;//导出这个模块/类,让其他js能引入

node.js调用模块的更多相关文章

  1. node.js基础模块http、网页分析工具cherrio实现爬虫

    node.js基础模块http.网页分析工具cherrio实现爬虫 一.前言      说是爬虫初探,其实并没有用到爬虫相关第三方类库,主要用了node.js基础模块http.网页分析工具cherri ...

  2. Node.js调用百度地图Web服务API的Geocoding接口进行点位反地理信息编码

    (从我的新浪博客上搬来的,做了一些修改.) 最近迷上了node.js以及JavaScript.现在接到一个活,要解析一个出租车点位数据的地理信息.于是就想到使用Node.js调用百度地图API进行解析 ...

  3. Node.js Net 模块

    Node.js Net 模块提供了一些用于底层的网络通信的小工具,包含了创建服务器/客户端的方法,我们可以通过以下方式引入该模块: var net = require("net") ...

  4. windows下node.js调用bat

    node.js调用bat需要用到Child Processes模块 因为bat是文件,所以需要使用execFile方法   如果指定了cwd,它会切换bat执行的目录,类似cd的功能,如果未指定默认为 ...

  5. 38..Node.js工具模块---底层的网络通信--Net模块

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js Net 模块提供了一些用于底层的网络通信的小工具,包含了创建服务器/ ...

  6. Node.js之模块机制

    > 文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. ![file](https://img2018.cnblogs.com/blog/830272/20 ...

  7. Node.js require 模块加载原理 All In One

    Node.js require 模块加载原理 All In One require 加载模块,搜索路径 "use strict"; /** * * @author xgqfrms ...

  8. node.js中模块和包

    node.js中模块和包 什么是模块 如何创建并加载模块 1. 创建模块 2. 单次加载 3. 覆盖 exports 如何创建一个包 1. 作为文件夹的模块 2. package.json 如何使用包 ...

  9. Node.js的模块载入方式与机制

    Node.js中模块可以通过文件路径或名字获取模块的引用.模块的引用会映射到一个js文件路径,除非它是一个Node内置模块.Node的内置模块公开了一些常用的API给开发者,并且它们在Node进程开始 ...

随机推荐

  1. CMA连续物理内存用户空间映射---(二)

    摘要: 相对于上一篇測试程序CMA连续物理内存用户空间映射---(一) 添加功能: 1.分配和映射统一放在IOCTL,一次完毕,能够连续多次分配并映射到用户空间,提高操作性: 2.驱动添加链表,使分配 ...

  2. 利用Bootstrap制作一个流行的网页

    首先是html承载内容: <!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset ...

  3. 让Qt Creator支持Windows Phone 8开发

    让Qt Creator支持Windows Phone 8开发 近期QtCreator3.2出了.修复了一些Bug.比上一个版本号3.1.2要好了一些. 因为在上一个版本号(Qt for WinRT自带 ...

  4. 我的superui开源后台bootstrap开发框架

    我的superui开源后台bootstrap开发框架:http://git.oschina.net/tzhsweet/superui

  5. ES标准中的相等比较算法 SameValue SameValueZero

    1.相等比较算法 The Abstract Equality Comparison Algorithm (==) The Strict Equality Comparison Algorithm (= ...

  6. 读写锁pthread_rwlock_t的使用(转)

    读写锁是用来解决读者写者问题的,读操作可以共享,写操作是排他的,读可以有多个在读,写只有唯一个在写,同时写的时候不允许读. 具有强读者同步和强写者同步两种形式 强读者同步:当写者没有进行写操作,读者就 ...

  7. linux c 获取网卡状态(UP or DOWN)

    源代码例如以下: #include <sys/socket.h> #include <sys/ioctl.h> #include <linux/if.h> #inc ...

  8. linux phpredisAdmin安装步骤

    1:linux安装apache环境, 这一步可以不用安装plsql http://www.cnblogs.com/lufangtao/archive/2012/12/30/2839679.html 2 ...

  9. spring启动加载过程源码分析

    我们知道启动spring容器两常见的两种方式(其实都是加载spring容器的xml配置文件时启动的): 1.在应用程序下加载 ApplicationContext ctx = new ClassPat ...

  10. tcpdump抓包(转)

    Linux 环境下,通常通过 tcpdump 来进行抓包和分析.它是几乎所有 Linux 发行版本预装的数据包抓取和分析工具. tcpdump 工具的获取和安装可以参阅相应操作系统的官方文档,本文不再 ...