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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:03:28+00:00 2026-06-04T12:03:28+00:00

So I have a pseudo class with functions and vars inside of it. For

  • 0

So I have a pseudo class with functions and vars inside of it. For example:

function MyClass()
{
    this.a = 0;
    this.b = function(c)
    {
        this.a += c;
    }
}

Then, when I go to use it later I’ll do this:

var myObject = new MyClass();
myObject.b(3);
myObject.b(5);

but when I do this:

console.log("A: " + myObject.a);

I get:

A: 0

What am I doing wrong?

Here’s my actual code. It’s split into mutiple files but I’ll put up the ones that are relevant:

function SongDatabase() {

    this.songs = new Array();

    this.loadSongs = function () {

    };

    this.saveSongs = function () {

    };

    var _Constructor_ = function () {
            var mySong = new Song();
            this.songs = new Array(mySong);
        };
    _Constructor_();
}

function LyriX() {
    var songDatabase = new SongDatabase();
    //var playlistDatabase = new PlaylistDatabase();
    songDatabase.loadSongs();
    var sourceList = new ScrollableList();
    sourceList.setElement($S.getElement("sourceList"));
    var accessoryList = new ScrollableList();
    accessoryList.setElement($S.getElement("accessoryList"));

    var sourceListClick = function (index) {
            $S.log("source click: " + index);
            if (index == 0) {
                displaySongs();
            }
        };
    sourceList.setClickListener(sourceListClick);

    var displaySongs = function () {
            $S.log("display songs");
            // STACK OVERFLOW LOOK HERE!!! thanks :)
            // in debug in chrome songDatabase.songs is a zero length array
            accessoryList.loadArray(songDatabase.songs);
        };
}

$S.addOnLoadListener(function () {
    new LyriX();
});
  • 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-04T12:03:29+00:00Added an answer on June 4, 2026 at 12:03 pm

    I see someone’s taking the “Java” in JavaScript too literally. 🙂

    A couple things you should know about JavaScript:

    1. In JavaScript, arrays don’t work like they do in other languages.
      They’re a lot more like dictionaries then what you would call an array in C or Java;
      they’re not significantly more memory efficient, or faster;
      no preallocation is done;
      there’s no offeset, etc, in the low-level implementation.
      JavaScript arrays are little more than a convenient (but useful!) structure
      for holding order-imperative data.

    2. Arrays can be created using the new Array(length) expression,
      or the simple literal expression, [].
      Generally, you’ll want to use the array literal, [].

    3. Using new Array(length) doesn’t really do anything useful;
      it sets the initial length property of the array, but that’s basically it.
      All elements remain undefined.
      There are no additional constraints or bounds checking.
      You can do a[100] = 'whatever' on an array created by calling var a = new Array(5)
      and the interpreter won’t bat an eye.

    4. JavaScript uses prototypal inheritance which is significantly different then the
      classical inheritance model used in languages like C++ and Java.

    With these points in mind, lets examine the following code block:

    function SongDatabase() {
        this.songs = new Array();
        this.loadSongs = function () {
            // etc.
        };
    
        this.saveSongs = function () {
            // etc.
        };
    
        var _Constructor_ = function () {
            var mySong = new Song();
            this.songs = new Array(mySong);
        };
        _Constructor_();
    }
    

    This block of code is probably not doing what you think it’s doing.

    1. By initializing the SongDatabase methods inside the SongDatabase() function
      you’re creating new method functions for every instance of songDatabase.
      This may not be a big deal when you’re dealing with a couple of dozen of instances,
      but if you’re dealing with hundreds, the extra memory required can become a problem.
      You’ll want to use the prototype pattern here, instead (see below).
    2. Your _Constructor_ function isn’t doing anything.
      var mySong = new Song() creates a new mySong object
      local to the _Constructor_ function and not accessible outside of it.
      When the _Constructor_ invocation returns,
      it’s mySong variable is garbage collected (like any other local variable would be).
    3. _Constructor_ is a private function and not a method;
      I’m not entirely sure what this in that context will reference.
      You may end up creating a songs property on the global object
      (but I’d want to test that to be sure).
    4. As I mentioned earlier, when you call Array() with the new operator,
      it takes an optional argument that sets the initial length of the array.
      In this case, the interpreter will try to coerce mySong into a number
      (mySong is not added to the array!);
      when that fails it will simply return a new array with length = 0.

    Instead, you’re better off writing SongDatabase() like so:

    function SongDatabase() {
        this.songs = [];
        // etc.
    }
    
    SongDatabase.prototype.loadSongs = function () {
        // load songs into `this.songs`
    };
    
    SongDatabase.prototype.saveSongs = function () {
        // save songs loaded into `this.songs`
    };
    

    The prototypal pattern may look strange,
    but its probably the best way to handle your use case.
    You’ll still have direct access to then songs array (which may or may not be important),
    and by attaching the loadSongs and saveSongs functions to SongDatabase‘s prototype
    you ensure that those functions are shared.

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

Sidebar

Related Questions

I have a Class the runs a process by itself Pseudo Class class MyClass
I have following situation. In a constructor of a pseudo class I attach a
Pseudo-situation: have a class (let's say BackgroundMagic ), and it has Start() and Stop()
I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
Hi I have below pseudo code with throws an exception like this throw new
in writing a scripting engine, I have functions like (psuedo-code) function is_whitespace?(char c){ return
I have this class that tries multiple methods of getting data from Google maps
I have this pseudo code to display different content based on user selection, which
I'm looking for a function, class or collection of functions that will assist in
I have a bunch of Javascript functions that look like the following: function generateBusinessImage

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.