Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9302763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:19:43+00:00 2026-06-18T23:19:43+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:19:45+00:00Added an answer on June 18, 2026 at 11:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to return a simple json result, something like: { 12323: true }
I want to do something like this without using extra variables: class className {
Good Day, I'm creating a VBScript function to return an array. But I want
I want to return just 1 item in the filtered array my code is
I'm using Array.sample to return a random element from an array. I want to
I want to return a set of values from function till the point they
Is there a way to return a non-associative array as a result of a
I want to extend Array.sort() to accept another parameter. This question has a solution
I want to return a specific message on error of an ajax request. [WebMethod]
I want to return a private field from a remoted object but i get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.