node.js(个人笔记)

原创 小兰子 随笔 个人笔记 416阅读 2017-08-28 17:40:49 举报

node命令行:
node-v:查看node安装版本号
node:进入交互环境
Ctrl+C:连续按两次退出交互环境
npm:node.js的包管理工具(package manager);
npm -v:正确安装
实现模块化:
module.exprots=hello;//暴露出需要引用的js方法
var foo=require('./hello');//引用hello.js
foo.greet(s)//使用方法

node基本模块
在Node.js环境中,唯一的全局对象叫global
在node交互环境下:

global.console
Console {
log: [Function: bound ],
info: [Function: bound ],
warn: [Function: bound ],
error: [Function: bound ],
dir: [Function: bound ],
time: [Function: bound ],
timeEnd: [Function: bound ],
trace: [Function: bound trace],
assert: [Function: bound ],
Console: [Function: Console] }

node进程

process === global.process;
true
process.version;
'v5.2.0'
process.platform;
'darwin'
process.arch;
'x64'
process.cwd(); //返回当前工作目录
'/Users/michael'
process.chdir('/private/tmp'); // 切换当前工作目录
undefined
process.cwd();
'/private/tmp'

node.js封装实例:
1.student.js
function add(student){
console.log('Add student:'+student);
}
exports.add=add;

2.teacher.js
function add(teacher){
console.log('Add Teacher:'+teacher);
}
exports.add=add;

3.kass,js
var student = require('./student');
var teacher=require('./teachers');
function add(teacherName,students){
teacher.add(teacherName);
students.forEach(function(item,index) {
student.add(item);
});
}
exports.add=add;

var klass=require('./klass')
exports.add=function(klasses){
klasses.forEach(function(item,index) {
var _klass=item;
var teacherName=item.teacherName;
var students=item.students;
klass.add(teacherName,students);
});
}
var klass=require('./klass')
klass.add('Scott',['白富美','高富帅','黑穷鬼']);

node -v
npm -v
cd school
node index.js

评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复