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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:42:09+00:00 2026-05-30T08:42:09+00:00

I have a JavaScript class defined as shown here: function Item() { this.init(); }

  • 0

I have a JavaScript class defined as shown here:

function Item() { 
  this.init(); 
} 

Item.prototype = { 
  id: null, 
  name: '', 
  description: '', 
  init: function () { 
    this.id = "1"; 
  } 
};

I want to add a property called “logs”. I want this property to be an array of other class entities. How do I define this kind of array as a property on my Item class? How do I add entities to that array? Originally, I thought I could add “this.logs = {};” in my init function. Then in my code elsewhere, I tried the following:

Item i = new Item();
i.logs.push(NewLog());

However, the push function through an error that says: “Cannot call method ‘push’ of undefined”

What am I doing wrong?

  • 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-05-30T08:42:10+00:00Added an answer on May 30, 2026 at 8:42 am

    logs is not initialized yet. Add it in the constructor (not to the prototype, because the array would then be shared by all instances of Item):

    function Item() { 
      this.init();
      this.logs = []; // Not {}:   [] is equivalent to new Array(), and
    }                 //           {} is equivalent to new Object() (not an array)
    

    Also, JavaScript != Java.

    var i = new Item() // instead of Item i = new Item();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Javascript class defined like so: var Welcomer = function(name) { var
I have a javascript function (class) that takes a function reference as one paremter.
I have in my javascript these 2 functions classes: // product class function Product()
first time using Asp.Net MVC here. I have a model class defined with 3
I have a simple javascript class. One method of this class sets up a
I have a very simple Javascript (jQuery) code, which just add a class name
I have a JavaScript class that I have made and put it into its
I have some javascript that goes out and fetches a javascript class on another
I have the following javascript class which I am trying to pass to an
Is there a best-practice or common way in JavaScript to have class members as

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.