我正在阅读有关Node.js文件系统的文档fs.writeFile(filename, data, [options], callback).所以我注意到我经常看到[选项],但从未用过任何东西.有人能举个例子吗?我所有的情况都没有使用这个选项.
ube*_*001 18
我猜你对optionsjavascript中的参数通常如何工作很感兴趣.
- 选项 对象
- 编码 字符串 | Null default ='utf8'
- mode Number默认= 438(八进制也称为0666)
- flag String default ='w'
通常,options参数是一个对象,其属性是您要修改的选项.因此,如果您想要修改其中的两个选项fs.writeFile,则需要将每个选项作为属性添加到options:
fs.writeFile(
"foo.txt",
"bar",
{
encoding: "base64",
flag: "a"
},
function(){ console.log("done!") }
)
Run Code Online (Sandbox Code Playgroud)
如果你对这三个参数的用途感到困惑,那么文档就fs.open可以提供你需要的一切.它包括所有可能性flag和描述mode.在callback被调用一次的writeFile操作完成.
对于最终在这里寻找标志参考的任何人,这里是:
Flag Description
r Open file for reading. An exception occurs if the file does not exist.
r+ Open file for reading and writing. An exception occurs if the file does not exist.
rs Open file for reading in synchronous mode.
rs+ Open file for reading and writing, asking the OS to open it synchronously. See notes for 'rs' about using this with caution.
w Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
wx Like 'w' but fails if the path exists.
w+ Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
wx+ Like 'w+' but fails if path exists.
a Open file for appending. The file is created if it does not exist.
ax Like 'a' but fails if the path exists.
a+ Open file for reading and appending. The file is created if it does not exist.
ax+ Like 'a+' but fails if the the path exists.