how to get object's [[DefaultValue]]

2019-09-08 04:50发布

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条回答
成全新的幸福
2楼-- · 2019-09-08 05:36

[[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"
查看更多
登录 后发表回答