how to get object's [[DefaultValue]]

蓝咒 提交于 2019-12-25 09:28:04

问题


according to ecma262-3 8.6.2.6 [DefaultValue]
http://bclary.com/2004/11/07/#a-8.6.2.6
now i want to get the [[DefaultValue]] of [ ]
so according to ecma,like this:
When the [[DefaultValue]] method of O is called with hint Number, the following steps are taken:
1. Call the [[Get]] method of object O with argument "valueOf".
[ ].valeOf() => [ ]//itself
2. If Result(1) is not an object, go to step 5.
[ ] is an object
3. Call the [[Call]] method of Result(1), with O as the this value and an empty argument list.
Result(1) => [ ],[ ] don't implement [[Call]]
4. If Result(3) is a primitive value, return Result(3).
so ,no Result(3),or it is still [ ]
5. Call the [[Get]] method of object O with argument "toString".
[ ].toString => ""
6. If Result(5) is not an object, go to step 9.
Result(5) => "" is not an object, go to step 9
7. Call the [[Call]] method of Result(5), with O as the this value and an empty argument list.
8. If Result(7) is a primitive value, return Result(7).
9. Throw a TypeError exception.

error? god!


回答1:


[[DefaultValue]] called on array object eventually gets to (and calls) array object's toString method. That method is essentially an Array.prototype.toString which is the same as calling Array.prototype.join on an array object (see 15.4.4.2). So toString on an empty array object returns empty string ("") which is a primitive value and is therefore returned from [[DefaultValue]] internal method.

So [[DefaultValue]] of array is an empty string — if Array.prototype.string is not overwritten/shadowed, and if Array.prototype.valueOf is not overwritten/shadowed.

[]+''; // ""

Array.prototype.toString = function(){return 1};
[]+''; // "1"

Array.prototype.valueOf = function(){return 2};
[]+''; // "2"


来源:https://stackoverflow.com/questions/5814949/how-to-get-objects-defaultvalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!