3

I want to return an array with extra methods. I usually do something like this:

MyConstructor = function(a, b, c){
    var result = [a, b, c];
    result.method1 = function(){
        return a + b + c ;
    }
    return result ;
}

var obj = MyConstructor(2, 4, 6); // removed `new`

But as the number of methods and uses grow, I believe it will be easier to maintain (and more efficient) to use the prototype instead of defining these new (identical) anonymous functions each time but I can't find a way to do this without those methods ending up on the Array.prototype.

Is this possible and if so, how?

4
  • 1) Your code is a factory method, and the new keyword is pointless for it. 2) #1 makes using the prototype chain pointless. Commented Feb 13, 2013 at 18:19
  • 1
    perfectionkills.com/… Commented Feb 13, 2013 at 18:27
  • Thank you. But even without it, I can't put methods on the prototype of the object that returns. Commented Feb 13, 2013 at 18:28
  • @ColBeseder because your code is a factory method, not a "Class". Therefore, adding stuff to the prototype of MyConstructor serves no purpose. See my answer. Commented Feb 13, 2013 at 18:29

2 Answers 2

2

One approach is to use a wrapper object, as in the answer by Shmiddty.

Or, if you don't want to use a wrapper but want to modify the array directly, you could just augment it:

// Define some special methods for use
var specialMethods = {
  sum: function() {
    var i = 0, len = this.length, result = 0;
    for (i; i < len; i++) result += this[i];
    return result;
  },
  average: function() {
    return this.sum() / this.length;
  }
};

function specialize(array) {
  var key;
  for (key in specialMethods) {
    if (specialMethods.hasOwnProperty(key)) {
      array[key] = specialMethods[key];
    }
  }
  return array;
}

var arr = specialize([1, 2, 3, 4, 5]);
console.log(arr.sum()); // 15
console.log(arr.average()); // 3

This way you don't touch Array.prototype and your methods get added to the array without having to redefine them over and over. Note that they do, though, get copied to each array, so there is some memory overhead - it's not doing prototypical lookup.

Also, keep in mind that you could always just define functions that operate on arrays:

function sum(array) {
  var i = 0, len = array.length, result = 0;
  for (i; i < len; i++) result += array[i];
  return result;
}

You don't get the syntax sugar of somearray.sum(), but the sum function is only ever defined once.

It all just depends on what you need/want.

Sign up to request clarification or add additional context in comments.

1 Comment

Syntax sugar is the whole point of the exercise! This approach is a good improvement on what I had. Bergi posted a nice link above, but those solutions aren't right for my needs.
2

Modifying your example to allow for use with the prototype chain would look like this:

// this could still be    var MyConstructor = function...
// but I prefer this form for Classes
function MyConstructor(a, b, c){
    this.result = [a, b, c];
}

MyConstructor.prototype.method1 = function(){
    // Array.reduce can be used for summation, or you could hardcode something like:
    // return this.result[0] + this.result[1] + this.result[2]
    // or do a summation through a for-loop, etc 
    return this.result.reduce(function(a,b){return a+b});
}

var obj = new MyConstructor(2, 4, 6);
console.log(obj.result, obj.method1()); // [2, 4, 6]  12

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.