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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:14:20+00:00 2026-06-02T21:14:20+00:00

I’ve just stumbled upon what I consider strange behavior which might well just be

  • 0

I’ve just stumbled upon what I consider strange behavior which might well just be my misunderstanding of how javascript inheritance works…

I’m using OpenLayers, which is based on prototypejs to create classes and subclasses. I expected that fields declared in the super class would become separate objects in each instantiation of the class or subclasses. This does not seem to be the case however, as you can see in the below example.

The super class MySuperClass defines the field collection however even though I construct multiple instances of this class, and via subclasses the field collection seems to be shared, as if it is static?

Why is it so, and how do I safely create an instance variable in the definition of a super class?

<script type="text/javascript" src="http://openlayers.org/dev/OpenLayers.js"></script>
<script type="text/javascript">
    OpenLayers.MySuperClass = OpenLayers.Class(
    {
        collection: [],

        initialize: function(options)
        {
            OpenLayers.Util.extend(this, options);
        },

        addItem: function(listener)
        {
            this.collection.push(listener);
        }
    });

    OpenLayers.MySubClassOne = OpenLayers.Class(OpenLayers.MySuperClass,
    {
        initialize: function(options)
        {
            OpenLayers.MySuperClass.prototype.initialize.apply(this, arguments);
        }
    });

    OpenLayers.MySubClassTwo = OpenLayers.Class(OpenLayers.MySuperClass,
    {
        initialize: function(options)
        {
            OpenLayers.MySuperClass.prototype.initialize.apply(this, arguments);
        }
    });

    var instanceOne = new OpenLayers.MySubClassOne();
    instanceOne.addItem("one");

    var instanceTwo = new OpenLayers.MySubClassTwo();
    instanceTwo.addItem("two");

    var instanceSuperOne = new OpenLayers.MySuperClass();
    instanceSuperOne.addItem("three");

    var instanceSuperTwo = new OpenLayers.MySuperClass();
    instanceSuperTwo.addItem("four");

    alert(instanceOne.collection);
    alert(instanceTwo.collection);
    alert(instanceSuperOne.collection);
    alert(instanceSuperTwo.collection);
</script>
  • 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-02T21:14:30+00:00Added an answer on June 2, 2026 at 9:14 pm

    I’m not really sure about this one, but I guess it is because you initialize the collection field in the class. Notice that the class is just an object, not a function. If you want it to be on instance level, you should initialize it in the constructor, in case of OpenLayers it’s initialize. I observed similar behaviour in jQuery UI and comming from an object-oriented language, I found this kind of confusing.

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

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:

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.