Object.prototype.valueOf()方法 [英] Object.prototype.valueOf() method

查看:134
本文介绍了Object.prototype.valueOf()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Object.prototype.valueOf.call("abc")
{ '0': 'a'
, '1': 'b'
, '2': 'c'
}
Object.prototype.valueOf.call(new String("abc"))
{ '0': 'a'
, '1': 'b'
, '2': 'c'
}

根据MDN,JavaScript调用valueOf方法将对象转换为原始值。如果对象没有原始值,则valueOf返回对象本身,显示为:
[object对象]
但是上面的valueOf以不同的格式返回而不是返回原语
{'0':'a'
,'1':' b'
,'2':'c'
}
。这不符合定义吗?为什么它以那种格式返回。现在,这造成了混乱。如果为数组和其他对象调用Object.prototype.valueOf.call(array),如何知道将返回什么。

According to MDN JavaScript calls the valueOf method to convert an object to a primitive value.If an object has no primitive value, valueOf returns the object itself, which is displayed as: [object Object] But above valueOf is returning in different format rather than returning primitive { '0': 'a' , '1': 'b' , '2': 'c' }.Isn't it against the definition?Why is it returning in that format .Now,this has created confusion .How to know what will get returned if Object.prototype.valueOf.call(array) is called for array and other objects.

同样为什么返回对象形式 {}。valueOf() g自从 []。valueOf()返回后显示的内容,当记录时不显示任何内容

Also why the return object form {}.valueOf() gets displayed since returns from [].valueOf() method when logged displays nothing

推荐答案

让我们来看看 valueOf (这是一个链接)确实:

Let's look at what valueOf (that's a link) does:


设O是调用ToObject传递此值作为参数的结果。

Let O be the result of calling ToObject passing the this value as the argument.

ToObject


字符串

String

创建一个新的String对象,其[[ PrimitiveValue]] internal属性设置为参数的值。有关String对象的说明,请参见15.5。

Create a new String object whose [[PrimitiveValue]] internal property is set to the value of the argument. See 15.5 for a description of String objects.

换句话说,它只是创建一个具有原始值的新字符串对象,即 new String('abc')。现在看看它在控制台中的显示方式,您会发现它与 .valueOf.call 结果相同。

In other words, it simply creates a new string object with the original value, i.e. new String('abc'). Now take a look at how that is displayed in your console, and you'll notice it's the same as the .valueOf.call result.

编辑:这实际上更多地与您用于查看答案的媒介有关。 Chrome和Firefox的开发工具将字符串显示为其文字值(字符串本身),但将字符串对象显示为常规对象(通过显示其属性)。

This actually has more to do with what medium you use to view the answer. Chrome's and Firefox's dev tools display strings as their literal values (the string itself), but display string objects as if they were regular objects (by displaying their properties).

A string只是一个字符的数组,上面有一些方法。所以表示 {'0':'a','1':'b','2':'c'} 表示 a 在第一个位置, b 在第二个位置, c 在第三个位置,是你要求的字符串。

A string is just an "array" of characters with some methods on them. So the representation {'0' : 'a', '1' : 'b', '2' : 'c'} means "a in the first position, b in the second, c in the third", which is the string you asked for.

作为最后一点, valueOf 不给 [object ObjectName] 。你可能指的是 Object.prototype.toString

And as a final note, valueOf does not give [object ObjectName]. You may be referring to Object.prototype.toString

这篇关于Object.prototype.valueOf()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆