Alan2
Alan2

Reputation: 24572

Is there a way I can combine functions and add them to more than one array in Javascript?

I have the following code:

$scope.fetching = [];
$scope.loading = [];
$scope.fetching.start = function (activity) {
    var index = this.indexOf(activity);
    if (index == 0)
        this.push(activity)
};
$scope.fetching.success = function (activity) {
    var index = this.indexOf(activity);
    if (index >= 0)
        this.splice(index, 1);
};
$scope.loading.start = function (activity) {
    var index = this.indexOf(activity);
    if (index == 0)
        this.push(activity)
};
$scope.loading.success = function (activity) {
    var index = this.indexOf(activity);
    if (index >= 0)
        this.splice(index, 1);
};

I store inside one array a list of all things that are loading and in another array a list of all things that are fetching. To use these I code something like the following:

$scope.fetching.start("Fetching new topics");
$scope.fetching.success("Fetching new topics");
$scope.loading.start("Loading new ideas");
$scope.loading.success("Loading new ideas");

Is there a way that I could make it so I would not have to reuse exactly the same code for the fetching and loading arrays?

Upvotes: 0

Views: 51

Answers (3)

MaxArt
MaxArt

Reputation: 22637

You may consider creating a class for your task:

function LoadArray() {
    // ...
}
LoadArray.prototype = [];
LoadArray.prototype.start = function (activity) {
    var index = this.indexOf(activity);
    if (index == 0)
        this.push(activity)
};
LoadArray.prototype.success = function (activity) {
    var index = this.indexOf(activity);
    if (index >= 0)
        this.splice(index, 1);
};

$scope.loading = new LoadArray();
$scope.fetching = new LoadArray();

This is supported even by IE6, in case you were wondering.

Upvotes: 0

Qantas 94 Heavy
Qantas 94 Heavy

Reputation: 16020

If the non-standard __proto__ property is supported, you could do something like this (JSFiddle):

function Test()
{   var a = [];
    a.__proto__ = Test.prototype;
    return a;
}

Test.prototype = Object.create(Array.prototype,
{ start:
    { enumerable: false
    , configurable: true
    , writable: true
    , value: function (activity)
        {   var index = this.indexOf(activity);
            if (index == 0) this.push(activity);
        }
    }
, success:
    { enumerable: false
    , configurable: true
    , writable: true
    , value: function (activity)
        {   var index = this.indexOf(activity);
            if (index >= 0) this.splice(index, 1);
        }
    }
});

$scope.fetching = Test();
$scope.loading = Test();

Upvotes: 1

Tibos
Tibos

Reputation: 27833

Since your array/objects have the same structure, you could write a function that builds objects of that type.

function buildModifiedArray() {
  var arr = [];
  arr.start = function (activity) {
    var index = this.indexOf(activity);
    if (index == 0)
        this.push(activity)
  };
  arr.success = function (activity) {
    var index = this.indexOf(activity);
    if (index >= 0)
        this.splice(index, 1);
  };
  return arr;
}

$scope.fetching = buildModifiedArray();
$scope.loading = buildModifiedArray();

Upvotes: 1

Related Questions