简体   繁体   English

Javascript toString对我没有意义

[英]Javascript toString making no sense to me

OK I just did it and scratched my head for a while. 好吧,我只是做了一下,挠了一下头。 I tried following on my Chrome console: 我在Chrome控制台上尝试了以下操作:

var a = [];
toString.call(a); //[object Array]
a.toString(); //""
toString(a); //[object Object] I know it's blunder but still!

What is difference between the toString and .toString I definitely know they are from different scopes (objects), but which one should be used at what time? toString和.toString有什么区别?我绝对知道它们来自不同的作用域(对象),但是应该在什么时候使用哪一个? Why is it so messy? 为什么这么乱?

First of all we have to clarify that toString refers to Object.prototype.toString : 首先,我们必须澄清toString是指Object.prototype.toString

> toString === Object.prototype.toString
  true

How Object.prototype.toString works is explained in section 15.2.4.2 of the specification : 在规范的15.2.4.2节中说明了Object.prototype.toString工作方式:

  1. If the this value is undefined , return "[object Undefined]" . 如果thisundefined ,则返回"[object Undefined]"
  2. If the this value is null , return "[object Null]" . 如果this值为null ,则返回"[object Null]"
  3. Let O be the result of calling ToObject passing the this value as the argument. O为调用ToObject并将this值作为参数传递的结果。
  4. Let class be the value of the [[Class]] internal property of O . classO[[Class]]内部属性的值。
  5. Return the String value that is the result of concatenating the three Strings "[object " , class , and "]" . 返回将三个字符串"[object "class"]"串联在一起的结果的String值。

toString.call(a) is the same as Object.prototype.toString.call(a) and works according to the above algorithm: this refers to the array a (because you used .call ), the internal [[Class]] property has the value Array , hence the output is [object Array] . toString.call(a)Object.prototype.toString.call(a)相同,并且根据上述算法工作: thisObject.prototype.toString.call(a)a (因为您使用了.call ),内部[[Class]]属性具有值Array ,因此输出为[object Array]

a.toString() : Arrays overwrite the toString property, which is defined in section 15.4.4.2 . a.toString() :数组将覆盖在15.4.4.2节中定义的toString属性。 In short, all array elements are concatenated and since the array is empty, you get an empty string as result. 简而言之,所有数组元素都是串联的,并且由于数组为空,因此您将得到一个空字符串。

toString(a) is the same as Object.prototype.toString() . toString(a)Object.prototype.toString()相同。 The argument is simply ignored. 该参数将被忽略。 Therefore this refers to Object.prototype , which is an object and according to the algorithm mentioned above, the output is [object Object] . 因此, this是指Object.prototype ,它是一个对象,根据上述算法,输出为[object Object] The output would be same for any value of a . 的任何值的输出都是相同a


which one should be used at what time? 什么时候应该使用哪一个?

That depends on what you want to do. 那要看你想做什么。 Personally I find none of the built-in toString functions particularly useful, besides for some quick and dirty debugging. 就个人而言,除了进行一些快速而肮脏的调试之外,我发现没有内置的toString函数特别有用。


toString.call(a) should be == a.toString() toString.call(a)应该== a.toString()

Well, Object.prototype.toString and Array.prototype.toString are simply two different methods, hence you get different results. 好吧, Object.prototype.toStringArray.prototype.toString只是两个不同的方法,因此您得到的结果是不同的。

You could argue that Object.prototype.toString should call the overwritten toString if it exists, but that's just not how toString is implemented. 您可能会争辩说Object.prototype.toString如果存在,应该调用覆盖的toString ,但这不是toString实现的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM