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 6028657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:46:21+00:00 2026-05-23T04:46:21+00:00

I was trying to make a constructor function and within it, i had/wanted to

  • 0

I was trying to make a constructor function and within it, i had/wanted to place an array:

function Animal(s) {
    this.myarr.push(s)
    this.bark = function() {
        console.log(this.myarr)
    }
}

 Animal.prototype = {
    myarr: []
}

var a = new Animal('a');
a.bark()

var b = new Animal('b');
b.bark()

The problem is that the array will be passed by reference. The result will be:

["a"]
["a", "b"]

I then tried Dean Edwards base class:

var Animal = Base.extend({
  constructor: function(s) {
    this.some.push(s)
  },
  some: [],

  bark: function() {
    console.log(this.some)
  }
});

var a = new Animal('a');
a.bark()

var b = new Animal('b');
b.bark()

The result was the same.

["a"]
["a", "b"]

I don’t know if this is the way arrays should be handler in a javascript ‘class’ system… though i can re-initialize that property to [] every time i call the constructor.

So, does anyone know how to deal with this? Should i re-initialize the property(if it’s an array)?
Thanks in advanced.

Edit:

The problem here is that whenever the constructor is called(this.myarr.push(s) for example) myarr will point to the same array – that’s why after a new instance the array will grow in size.

So:

var a = new Animal('a')
var b = new Animal('ab')
var c = new Animal('abc')
var d = new Animal('abcd')

will result to:

["a", "ab", "abc", "abcd"]

I want(without defining myarr inside the constructor) myarr to have a default value everytime i call the constructor(through new Animal(()) which should be [ ].

  • 1 1 Answer
  • 2 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-05-23T04:46:21+00:00Added an answer on May 23, 2026 at 4:46 am

    If you attach your array to the prototype of the constructor, this array will be available to all instances, so everything an instance does to it, is reflected in .myarr in the other instances. If you want an array per instance, you should define it as a ‘local’ property, i.e. this.myarr. What you can do is define a method in the prototype that pushes a value into this.myarr.

    So your constructor function (classes are non extistent in javascript) could look like:

    function Animal(s) {
        this.myarr = [];
        this.bark = function() {
            console.log(this.myarr)
        }
        this.myarrAdd(s);
    }
    
     Animal.prototype = {
        myarrAdd: function(val){this.myarr.push(val); return this;}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to write a constructor function that would make an object
trying to make a page which will recursively call a function until a limit
Im trying to make a function that will return an element of type point:
I'm encountering a big problem when i'm trying to make a recursive search function
I'm trying to make a class that uses the Levenshtein distance function to compare
I'm trying to make a simple Vector class (math) this way: template <int D,
I'm trying to make the add_day function work, but I'm having some trouble. Note
Let me make this disclaimier : I have clear understanding of virtual function call
I'm trying to make the constructor abort the object construction if something fails, for
I have been trying to make this work for a long time now. In

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.