[英]Node type checking / 'coerced to a string'?
我刚接触Node.js时,就会知道节点类型是如何工作的...
我目前正在处理文件,仅举一个例子看看这个功能:
fs.write(fd, data[, position[, encoding]], callback)
根据文档 ,数据应为以下类型:
data <String> | <Buffer>
但是接下来指定
If data is not a Buffer instance then the value will be coerced to a string.
1 /我问,如果数据 paramater接受的类型是纯粹的投机,它可以是任何类型像: Object | MyOwnObject
Object | MyOwnObject
?
2 /在这种情况下, “强制转换为字符串”是什么意思? 我的意思是,它在调用我的对象的toString()方法吗?
谢谢。
如果data
参数既不是Buffer
也不是String
,则通过向其添加空字符串来强制它:
if (typeof buffer !== 'string')
buffer += '';
这相当于
buffer = String(buffer) + '';
在许多情况下,这最终将调用buffer.toString()
。
源码: 源代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.