[英]Node type checking / 'coerced to a string'?
Newly to Node.js i would have known how node types really working ... 我刚接触Node.js时,就会知道节点类型是如何工作的...
I'm currently working on files, just to have an example take a look at this function : 我目前正在处理文件,仅举一个例子看看这个功能:
fs.write(fd, data[, position[, encoding]], callback)
According to the documentation , data should be of types : 根据文档 ,数据应为以下类型:
data <String> | <Buffer>
but next it's specified 但是接下来指定
If data is not a Buffer instance then the value will be coerced to a string.
1 / I'm asking if data paramater accepted types is pure speculation and it could be of any type like: Object | MyOwnObject
1 /我问,如果数据 paramater接受的类型是纯粹的投机,它可以是任何类型像:
Object | MyOwnObject
Object | MyOwnObject
? Object | MyOwnObject
?
2 / In this case, what means 'coerced to a string' ? 2 /在这种情况下, “强制转换为字符串”是什么意思? I mean, Is it calling a toString() method of my object ?
我的意思是,它在调用我的对象的toString()方法吗?
Thanks. 谢谢。
If the data
parameter is neither a Buffer
nor a String
, it is coerced by adding the empty string to it: 如果
data
参数既不是Buffer
也不是String
,则通过向其添加空字符串来强制它:
if (typeof buffer !== 'string')
buffer += '';
This is equivalent to 这相当于
buffer = String(buffer) + '';
In many cases, this will end up calling buffer.toString()
. 在许多情况下,这最终将调用
buffer.toString()
。
Source: the source code 源码: 源代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.