sách gpt4 ăn đã đi

javascript - 为什么 toString.call ('my string' ) 不同于 'my string' .toString(),或者,为什么 document.toString !== 'asd' .toString?

In lại Tác giả: Walker 123 更新时间:2023-11-29 18:24:14 28 4
mua khóa gpt4 giày nike

正在阅读 Underscore.js 以了解它的 is[String|Number|...] 方法是如何工作的,现在我很困惑。下划线:

toString.call(obj) == ['object ' + name + ']';

好吧,我可以的

>>> toString.call('my string')
"[object String]"

Nhưng

>>> 'my string'.toString()
"my string"

我在这里迷路了!在我接到的第一个电话中:

>>> document.toString === toString
ĐÚNG VẬY

>>> document.toString === 'asd'.toString
false

所以,我很困惑。我没想到会有这种行为。

câu trả lời hay nhất

那是因为:

document.toString === Object.prototype.toString

它实现了最基本的 版本,类似于:

'[object ' + (typeof this) + ']';

这与 String.toString() 非常不同,它只是输出字符串本身,即:

> String.prototype.toString.call('hello world')
"hello world"

hoặc Array.toString() 输出逗号分隔的值字符串。

> Array.prototype.toString.call([1,2,3])
"1,2,3"

sử dụng.call()

为了完成这项工作,他们基本上通过使用 .call() Sẽ toString() 应用于对象:

toString.call(obj)

hiện hữu 方法中,cái này 现在引用obj。这将是等价的:

Object.prototype.toString.call(obj)

关于javascript - 为什么 toString.call ('my string' ) 不同于 'my string' .toString(),或者,为什么 document.toString !== 'asd' .toString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15182623/

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress