将isEmpty添加到数组原型

时间:2019-01-13 14:17:05

标签: javascript arrays prototype

我想向对象Array添加一个属性。我一直在尝试以下方法:

Array.prototype.isEmpty = array => (array || []).length <= 0 // only works if I use array.isEmpty(array) which isn't exactly what I want.

Array.prototype.isEmpty = (() => (this || []).length <= 0)() // always returns true since this is always undefined.

有没有办法在JavaScript中添加它?

0 个答案:

没有答案