javascript中调用函数的输出

javascript中调用函数的输出,javascript,Javascript,为什么这与a.toString()不同?在这两种情况下,toString函数中的this的值都是a,对吗?我希望它输出“asd”(与a.toString())相同)。您使用的是窗口。toString,但它应该是: var a = "asd"; toString.call(a); //prints [object String] 它们是不同的方法(尽管名称相同)。此外,为了证明toString在其他类型中的行为不同: String.prototype.toString.call(a) // t

为什么这与
a.toString()不同?在这两种情况下,toString函数中的
this
的值都是a,对吗?我希望它输出“asd”(与
a.toString()
)相同)。

您使用的是
窗口。toString
,但它应该是:

var a = "asd";
toString.call(a); //prints [object String]

它们是不同的方法(尽管名称相同)。此外,为了证明
toString
在其他类型中的行为不同:

String.prototype.toString.call(a)  // then it should be same
这也不会返回
“abc”


不是“abc”


错误,并且数字中的
toString
也可以有基数参数



结论:它们都是不同的。由于
window
是一种奇怪的
对象
,因此它与
String
toString不同。

window.toString()与String.prototype.toString()有何不同?是否有任何文档解释window.toString()的行为?它们只是不同的实现。@xdazz-我相信
window
中的
toString
String
中的
toString
具有不同的本机代码,就像
Array
节点列表中的
forEach
一样
[].toString.call("abc");  //Array
document.querySelectorAll("*").toString.call("abc")  //Node List
(2).prototype.toString.call("abc")  //Number