您的位置:首页 > Web前端 > Node.js

nodejs Tips7

2016-01-12 22:23 507 查看
13、

js的面向对象是基于原型的,并没有提供对象继承的语言特性,继承是通过原型复制来实现的。

继承仅仅继承原型链上的,构造函数内定义的属性和函数没有被继承。

//16.1.12.js
var util = require('util');

function Base() {
this.x = 10;
this.y = 5;
this.add = function() {
console.log("求和值为: " + (this.x + this.y));
}
}

Base.prototype.muli = function() {
console.log("乘法值为: " + (this.x * this.y));
}

function iBase() {
this.x = 11;
this.y = 12;
}

util.inherits(iBase, Base);
var b = new Base();
console.log(b);
b.add();
b.muli();

var c = new iBase();
console.log(c);
c.muli();

//***********************
//util.inspect并不会简单地直接把对象转换为字符串,即使该对象定义了toString方法也不会调用。
console.log(util.inspect(b, true));

//util.isArray(obj) 判断一个对象是不是数组
//util.isRegExp(object) 判断一个对象是不是正则表达式
//util.isDate(object) 判断一个对象是不是日期


nodejs 文件操作fs

//16.1.11.3_1.js

//nodejs file文件操作
var fs = require('fs');
var buf = new Buffer(1024);

//异步读取
fs.readFile('input.txt', function(err, data) {
if (err) {
return console.error(err);
}

console.log("异步读取: " + data.toString());
});

//同步读取
var data = fs.readFileSync('input.txt');
console.log("同步读取: " + data.toString());

console.log("Part1 End");

//
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}

fs.read(fd, buf, 0, buf.length, 0, function(err, bytes) {
if (err) {
console.log(err);
}
// 仅输出读取的字节
if (bytes.length > 0) {
console.log(buf.slice(0, bytes).toString());

}
//关闭文件
fs.close(fd, function(err) {
if (err) {
console.log(err);
}
console.log("文件关闭成功");
})
});
});

//fs.ftruncate(fd, len, callback)  异步模式下截取文件
//fs.unlink(path, callback)  删除文件
//fs.mkdir(path[, mode], callback)  创建目录
//fs.rmdir(path, callback)

fs.mkdir('/test/tmp', function() {
console.log("创建目录成功");
});

fs.rmdir('/test/tmp', function() {
console.log("删除目录成功");
});



文件模块方法参考手册

以下为 Node.js 文件模块相同的方法列表:
序号方法 & 描述
1fs.rename(oldPath, newPath, callback)

异步 rename().回调函数没有参数,但可能抛出异常。
2fs.ftruncate(fd, len, callback)

异步 ftruncate().回调函数没有参数,但可能抛出异常。
3fs.ftruncateSync(fd, len)

同步 ftruncate()
4fs.truncate(path, len, callback)

异步 truncate().回调函数没有参数,但可能抛出异常。
5fs.truncateSync(path, len)

同步 truncate()
6fs.chown(path, uid, gid, callback)

异步 chown().回调函数没有参数,但可能抛出异常。
7fs.chownSync(path, uid, gid)

同步 chown()
8fs.fchown(fd, uid, gid, callback)

异步 fchown().回调函数没有参数,但可能抛出异常。
9fs.fchownSync(fd, uid, gid)

同步 fchown()
10fs.lchown(path, uid, gid, callback)

异步 lchown().回调函数没有参数,但可能抛出异常。
11fs.lchownSync(path, uid, gid)

同步 lchown()
12fs.chmod(path, mode, callback)

异步 chmod().回调函数没有参数,但可能抛出异常。
13fs.chmodSync(path, mode)

同步 chmod().
14fs.fchmod(fd, mode, callback)

异步 fchmod().回调函数没有参数,但可能抛出异常。
15fs.fchmodSync(fd, mode)

同步 fchmod().
16fs.lchmod(path, mode, callback)

异步 lchmod().回调函数没有参数,但可能抛出异常。Only available on Mac OS X.
17fs.lchmodSync(path, mode)

同步 lchmod().
18fs.stat(path, callback)

异步 stat(). 回调函数有两个参数 err, stats,stats 是 fs.Stats 对象。
19fs.lstat(path, callback)

异步 lstat(). 回调函数有两个参数 err, stats,stats 是 fs.Stats 对象。
20fs.fstat(fd, callback)

异步 fstat(). 回调函数有两个参数 err, stats,stats 是 fs.Stats 对象。
21fs.statSync(path)

同步 stat(). 返回 fs.Stats 的实例。
22fs.lstatSync(path)

同步 lstat(). 返回 fs.Stats 的实例。
23fs.fstatSync(fd)

同步 fstat(). 返回 fs.Stats 的实例。
24fs.link(srcpath, dstpath, callback)

异步 link().回调函数没有参数,但可能抛出异常。
25fs.linkSync(srcpath, dstpath)

同步 link().
26fs.symlink(srcpath, dstpath[, type], callback)

异步 symlink().回调函数没有参数,但可能抛出异常。 type 参数可以设置为 'dir', 'file', 或 'junction' (默认为 'file') 。
27fs.symlinkSync(srcpath, dstpath[, type])

同步 symlink().
28fs.readlink(path, callback)

异步 readlink(). 回调函数有两个参数 err, linkString。
29fs.realpath(path[, cache], callback)

异步 realpath(). 回调函数有两个参数 err, resolvedPath。
30fs.realpathSync(path[, cache])

同步 realpath()。返回绝对路径。
31fs.unlink(path, callback)

异步 unlink().回调函数没有参数,但可能抛出异常。
32fs.unlinkSync(path)

同步 unlink().
33fs.rmdir(path, callback)

异步 rmdir().回调函数没有参数,但可能抛出异常。
34fs.rmdirSync(path)

同步 rmdir().
35fs.mkdir(path[, mode], callback)

S异步 mkdir(2).回调函数没有参数,但可能抛出异常。 mode defaults to 0777.
36fs.mkdirSync(path[, mode])

同步 mkdir().
37fs.readdir(path, callback)

异步 readdir(3). 读取目录的内容。
38fs.readdirSync(path)

同步 readdir().返回文件数组列表。
39fs.close(fd, callback)

异步 close().回调函数没有参数,但可能抛出异常。
40fs.closeSync(fd)

同步 close().
41fs.open(path, flags[, mode], callback)

异步打开文件。
42fs.openSync(path, flags[, mode])

同步 version of fs.open().
43fs.utimes(path, atime, mtime, callback)

44fs.utimesSync(path, atime, mtime)

修改文件时间戳,文件通过指定的文件路径。
45fs.futimes(fd, atime, mtime, callback)

46fs.futimesSync(fd, atime, mtime)

修改文件时间戳,通过文件描述符指定。
47fs.fsync(fd, callback)

异步 fsync.回调函数没有参数,但可能抛出异常。
48fs.fsyncSync(fd)

同步 fsync.
49fs.write(fd, buffer, offset, length[, position], callback)

将缓冲区内容写入到通过文件描述符指定的文件。
50fs.write(fd, data[, position[, encoding]], callback)

通过文件描述符 fd 写入文件内容。
51fs.writeSync(fd, buffer, offset, length[, position])

同步版的 fs.write()。
52fs.writeSync(fd, data[, position[, encoding]])

同步版的 fs.write().
53fs.read(fd, buffer, offset, length, position, callback)

通过文件描述符 fd 读取文件内容。
54fs.readSync(fd, buffer, offset, length, position)

同步版的 fs.read.
55fs.readFile(filename[, options], callback)

异步读取文件内容。
56fs.readFileSync(filename[, options])<br同步版的 fs.readfile.<="" td="">
57fs.writeFile(filename, data[, options], callback)

异步写入文件内容。
58fs.writeFileSync(filename, data[, options])

同步版的 fs.writeFile。
59fs.appendFile(filename, data[, options], callback)

异步追加文件内容。
60fs.appendFileSync(filename, data[, options])

The 同步 version of fs.appendFile.
61fs.watchFile(filename[, options], listener)

查看文件的修改。
62fs.unwatchFile(filename[, listener])

停止查看 filename 的修改。
63fs.watch(filename[, options][, listener])

查看 filename 的修改,filename 可以是文件或目录。返回 fs.FSWatcher 对象。
64fs.exists(path, callback)

检测给定的路径是否存在。
65fs.existsSync(path)

同步版的 fs.exists.
66fs.access(path[, mode], callback)

测试指定路径用户权限。
67fs.accessSync(path[, mode])

同步版的 fs.access。
68fs.createReadStream(path[, options])

返回ReadStream 对象。
69fs.createWriteStream(path[, options])

返回 WriteStream 对象。
70fs.symlink(srcpath, dstpath[, type], callback)

异步 symlink().回调函数没有参数,但可能抛出异常。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: