jimm101
jimm101

Reputation: 998

Why does ES6 define map.length==0?

According to MDN:

Map.length

The value of the length property is 0.

What is the use case for this? I understand why Map.size is semantically correct. But surely a Map.length that almost always returns the "wrong" answer is a bad idea, especially if there's an oversight migrating code from ES5. Is there a way to force an error when this is used?

Upvotes: 8

Views: 1768

Answers (1)

GOTO 0
GOTO 0

Reputation: 47791

Constructors in JavaScript are regular functions, and the length property of a function corresponds to the number of formal parameters expected by the function, which is 0 in the case of Map.

Contrast this with RegExp.length, which is 2, because the RegExp constructor expects two parameters (pattern and flags).

Upvotes: 15

Related Questions