簡體   English   中英

節點類型檢查/“強制為字符串”?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM