1

Given Code

var isEmptyArray = function (array) {
   if (typeof array !== 'undefined' && array.length > 0) {
}

Usage

isEmptyArray(myArray);

Wanted Result

How can I re-write above to be able to use:

myArray.isEmptyArray();
Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147

1 Answers1

4

Just javascript:

Array.prototype.isEmptyArray = function() {
    return this.length === 0;
}

Typescript:

interface Array<T> {
    isEmptyArray(): boolean;
}

Array.prototype.isEmptyArray = function() {
    return this.length === 0;
}

Edit

The above solution will work for all instances of Array, for example:

let a = [];
console.log(a.isEmptyArray()); // true
a.push(1);
console.log(a.isEmptyArray()); // false

You can create your own array class and then implement needed methods just there (without affecting other Array instances):

class MyArray<T> extends Array<T> {
    public isEmptyArray(): boolean {
        return this.length === 0;
    }
}

let a1 = [];
console.log(a1.isEmptyArray()); // Uncaught TypeError: a.isEmptyArray is not a function

let a2 = new MyArray<any>();
console.log(a2.isEmptyArray()); // true

This approach is good when you're using other js libraries which are not aware of the changes you've made in the Array prototype.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • 1
    It has nothing to do with Angular, it's just a matter of having the method in all array instances or just specific ones. I updated my answer. – Nitzan Tomer Jul 10 '16 at 12:55
  • 1
    This approach is bad because you're augmenting `Array`'s prototype with a completely trivial operation. Surely it's more performant and equally readable to simply inline `if(arr.length === 0)`? If you aren't sure if `arr` exists, simply use `if(arr && arr.length)`... – linguamachina Jul 11 '16 at 07:25
  • @headeronly In Object Oriented, the point is to have the ability to add methods to your classes. If you check a lot whether or not an array is empty then it makes much sense to put that into a method – Nitzan Tomer Jul 11 '16 at 07:49
  • @NitzanTomer This is not an OO vs. functional programming issue. Mutating the prototype of built-in objects has a bad smell about it. You are modifying a standardised, well-documented API for the sake of adding a *trivial* helper function. It's *not* "your" class. See http://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice – linguamachina Jul 11 '16 at 07:57
  • @headeronly It depends on the scenario. And I wrote that in my answer, if there are no scripts in your page that might get broken because of that then there's no reason why not. – Nitzan Tomer Jul 11 '16 at 08:05
  • @NitzanTomer Fair enough. I just think that `if(arr.length)` is faster and just as clear as `if(arr.isEmptyArray())`, if not more so. If you advise someone that it's OK to decorate a built-in prototype for something so trivial, it gives off the impression that they're free to do this for every little helper function. Also, "This approach is good when you're using other js libraries" is just bad advice. Unless you **know** that you're **not** using external libraries I think it's a bad idea to mutate built-ins. – linguamachina Jul 11 '16 at 08:11
  • @headeronly The `"this approach is good when you're using other js libraries"` part was meant for when you extend the array instead of changing it's prototype (which is what you're saying). I think that `if (array.empty())` is more clear and semantic than `if (arr.length === 0)`, but that's just my opinion. – Nitzan Tomer Jul 11 '16 at 08:18