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

  • SEARCH
  • Home
  • 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 8189825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:24:49+00:00 2026-06-07T03:24:49+00:00

i try to extend Array object in javascript with some user friendly methods like

  • 0

i try to extend Array object in javascript with some user friendly methods like Array.Add() instead Array.push() etc…

i implement 3 ways to do this.
unfortunetly the 3rd way is not working and i want to ask why? and how to do it work.

//------------- 1st way
Array.prototype.Add=function(element){
     this.push(element);
};

var list1 = new Array();
list1.Add("Hello world");
alert(list1[0]);

//------------- 2nd way
function Array2 () {
    //some other properties and methods
};

Array2.prototype = new Array;
Array2.prototype.Add = function(element){
  this.push(element);  
};

var list2 = new Array2;
list2.Add(123);
alert(list2[0]);

//------------- 3rd way
function Array3 () {
    this.prototype = new Array;
    this.Add = function(element){
      this.push(element);  
    };
};

var list3 = new Array3;
list3.Add(456);  //push is not a function
alert(list3[0]); // undefined

in 3rd way i want to extend the Array object internally Array3 class.
How to do this so not to get “push is not a function” and “undefined”?

Here i add a 4th way.

//------------- 4th way
function Array4 () {
    //some other properties and methods
    this.Add = function(element){
        this.push(element);
    };
 };
Array4.prototype = new Array();

var list4 = new Array4();
list4.Add(789);
alert(list4[0]);

Here again i have to use prototype.
I hoped to avoid to use extra lines outside class constructor as Array4.prototype.
I wanted to have a compact defined class with all pieces in one place.
But i think i cant do it otherwise.

  • 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-07T03:24:51+00:00Added an answer on June 7, 2026 at 3:24 am

    Method names should be lowercase. Prototype should not be modified in the constructor.

    function Array3() { };
    Array3.prototype = new Array;
    Array3.prototype.add = Array3.prototype.push
    

    in CoffeeScript

    class Array3 extends Array
       add: (item)->
         @push(item) 
    

    If you don’t like that syntax, and you HAVE to extend it from within the constructor,
    Your only option is:

    // define this once somewhere
    // you can also change this to accept multiple arguments 
    function extend(x, y){
        for(var key in y) {
            if (y.hasOwnProperty(key)) {
                x[key] = y[key];
            }
        }
        return x;
    }
    
    
    function Array3() { 
       extend(this, Array.prototype);
       extend(this, {
          Add: function(item) {
            return this.push(item)
          }
    
       });
    };
    

    You could also do this

    ArrayExtenstions = {
       Add: function() {
    
       }
    }
    extend(ArrayExtenstions, Array.prototype);
    
    
    
    function Array3() { }
    Array3.prototype = ArrayExtenstions;
    

    In olden days, ‘prototype.js’ used to have a Class.create method. You could wrap all this is a method like that

    var Array3 = Class.create(Array, {
        construct: function() {
    
        },    
        Add: function() {
    
        }
    });
    

    For more info on this and how to implement, look in the prototype.js source code

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

Sidebar

Related Questions

Whenever I try to extend the Object prototype, I get an error: Error #1056:
I try to extend the CheckfrontAPI class with my new class. In my case
I'm getting the following error when I try to extend SalatDAO or use grater[T].asObject(x):
Try executing the following in JavaScript: parseInt('01'); //equals 1 parseInt('02'); //equals 2 parseInt('03'); //equals
try: spam.foo except AttributeError: do_somthing() (Is it wise to check an attribute like that
Is there a way to pick a random object from an array of objects?
I try to show username and role of user when login success this I
I try to output some sound with Scala. My problem is that i get
I'm trying to convert an Image object to a byte array then back to
Possible Duplicate: Call to a member function on a non-object I try to call

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.