TypeError: invalid Array.prototype.sort argument - JavaScript 编辑
The JavaScript exception "invalid Array.prototype.sort argument" occurs when the argument of Array.prototype.sort()
isn't either undefined
or a function which compares its operands.
Message
TypeError: argument is not a function object (Edge) TypeError: invalid Array.prototype.sort argument (Firefox)
Error type
What went wrong?
The argument of Array.prototype.sort()
is expected to be either undefined
or a function which compares its operands.
Examples
Invalid cases
[1, 3, 2].sort(5); // TypeError
var cmp = { asc: (x, y) => x >= y, dsc: (x, y) => x <= y };
[1, 3, 2].sort(cmp[this.key] || 'asc'); // TypeError
Valid cases
[1, 3, 2].sort(); // [1, 2, 3]
var cmp = { asc: (x, y) => x >= y, dsc: (x, y) => x <= y };
[1, 3, 2].sort(cmp[this.key || 'asc']); // [1, 2, 3]
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论