简体   繁体   English

节点类型检查/“强制为字符串”?

[英]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.

相关问题 数组字面量:此类型不能强制为字符串 - array literal: This type can not be coerced to string 警告:提供的 `src` 属性是不受支持的类型 ImageUrlBuilder。 在此处使用之前,必须将此值强制转换为字符串 - Warning: The provided `src` attribute is an unsupported type ImageUrlBuilder. This value must be coerced to a string before before using it here JS:“false”可以强制类型为 false 吗? - JS: Can “false” be type-coerced to false? 扩展js原型,参数被强制类型化 - Extending js prototype, argument is getting type coerced JavaScript通过字符串比较检查类型的方式是否会影响性能? - Does JavaScript's way of checking type by string comparison affects performance? 在检查某些东西是否是字符串时可以进行类型转换比较吗? - Is it ok to do a type-converting comparison when checking if something is a string? 如何将JSON字符串转换为JavaScript对象,包括类型检查 - How to convert a JSON string into a JavaScript Object including type checking Typescript严格类型检查无法在可观察到的字符串和数组中使用 - Typescript strict-type checking not working with string and array in observable 将字符串转换为类型(node.js) - Converting string to a type (node.js) 如何在节点js中将字符串类型的输入转换为类型对象数组 - How to convert an input of type string into type object array in node js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM