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. mssql性能优化

    总结下SQL SERVER数据库性能优化相关的注意事项,在网上搜索了一下,发现很多文章,有的都列出了上百条,但是仔细看发现,有很多似是而非或者过时(可能对SQL SERVER6.5以前的版本或者ORA ...

  2. Mycat探索之旅(4)----Mycat的自增长主键和返回生成主键ID的实现

    说明:MyCAT自增长主键和返回生成主键ID的实现 1) mysql本身对非自增长主键,使用last_insert_id()是不会返回结果的,只会返回0:这里做一个简单的测试 创建测试表 ------ ...

  3. Unity3D教程宝典之Web服务器篇:(第二讲)从服务器下载图片

    转载自风宇冲Unity3D教程学院                                    从Web服务器下载图片 上一讲风宇冲介绍了wamp服务器及安装.这回介绍如何从服务器下载内容至 ...

  4. hibernate.cfg.xml文件连接mySql、Oracle、SqlServer配置

    1.连接mySql,文件配置如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibe ...

  5. Template design pattern application in android

    The template method allow separate the generic method from a detail context(class) via inheritance A ...

  6. 解决Linux下3T硬盘分区只有2T(2199G)可用

    分区转换成GPT即可 sudo parted /dev/sdb 将MBR硬盘格式化为GPT mklabel gpt 之后可以看一下状态 print 整个硬盘空间只分一个区 mkpart primary ...

  7. iOS开发-简单获取View截图图像(Quartz2D)

    1. 先指定图像的大小 UIGraphicsBeginImageContext(view.frame.size); 2. 在指定的区域绘制图像 [view drawViewHierarchyInRec ...

  8. 打开eclipse中文件所在文件夹

    在myeclipse中选中文件后能够打开文件所在文件夹,可是eclipse中没有直接打开文件路径的功能.须要我们自己加入. 选择:Run -> External Tools -> Exte ...

  9. oracle加入not null约束

    在创建表时.为列加入not null约束,形式例如以下: column_name data_type [constraint constraint_name] not null 当中,constrai ...

  10. Vue 获取验证码倒计时组件

    子组件 <template> <a class="getvalidate":class="{gray: (!stop)}"@click='cl ...