正在阅读 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
Và
>>> document.toString === 'asd'.toString
false
所以,我很困惑。我没想到会有这种行为。
那是因为:
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)
Tôi là một lập trình viên xuất sắc, rất giỏi!