Nodejs模塊的調(diào)用操作實例分析
更新時間:2018年12月25日 08:42:08 作者:ChouCat
這篇文章主要介紹了Nodejs模塊的調(diào)用操作,結(jié)合實例形式分析了nodejs模塊的定義與調(diào)用相關(guān)操作技巧,需要的朋友可以參考下
本文實例講述了Nodejs模塊的調(diào)用操作。分享給大家供大家參考,具體如下:
User.js
//構(gòu)造方法 function User(id, name, age) { this.id = id; this.name = name; this.age = age; this.enter = function () { console.log(this.name + "進入國家圖書館"); } } /* function User() { this.id; this.name; this.age; this.enter = function() { console.log(this.name + "進入圖書館"); } } */ module.exports = User;
Teacher.js
var User = require('./User'); function Teacher(id, name, age) { User.apply(this, [id, name, age]);//類的繼承 this.teach = function(res) { res.write(this.name + "講課"); } } module.exports = Teacher;
modalcall_1.js
//----------------------n3_modalcall.js模塊的調(diào)用------------- var http = require('http'); var User = require('./model/User'); 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, "jack", 20); //user.id = 1; //user.name = "張三"; //user.age = 20; user.enter(); response.end(''); } }).listen(8000); console.log('Server running at http://127.0.0.1:8000/');
modalcall_2.js
//----------------------n3_modalcall.js------------- var http = require('http'); var Teacher = require('./model/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, "JackLi", 20); teacher.enter(); teacher.teach(response); response.end(''); } }).listen(8000); console.log('Server running at http://127.0.0.1:8000/');
希望本文所述對大家nodejs程序設(shè)計有所幫助。
相關(guān)文章
node.js 利用流實現(xiàn)讀寫同步,邊讀邊寫的方法
下面小編就為大家?guī)硪黄猲ode.js 利用流實現(xiàn)讀寫同步,邊讀邊寫的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09使用node.js半年來總結(jié)的 10 條經(jīng)驗
從3月初來到帝都某創(chuàng)業(yè)公司的服務(wù)器團隊實習,到現(xiàn)在已接近半年的時間。PS: 已轉(zhuǎn)正,服務(wù)器端用的 Node。2014-08-08學習使用grunt來打包JavaScript和CSS程序的教程
這篇文章主要介紹了學習使用grunt來打包JavaScript和CSS程序的教程,grunt基于node.js和需要的朋友可以參考下2016-01-01node.js中使用q.js實現(xiàn)api的promise化
這篇文章主要介紹了node.js中使用q.js實現(xiàn)api的promise化,promise一個標準,它描述了異步調(diào)用的返回結(jié)果,包括正確返回結(jié)果和錯誤處理,需要的朋友可以參考下2014-09-09