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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:05:14+00:00 2026-05-24T20:05:14+00:00

I’m currently working on some code based on John Resig – Simple JavaScript Inheritance

  • 0

I’m currently working on some code based on John Resig –Simple JavaScript Inheritance.
And i’m having some trouble with the initialization of arrays. If i put an array as an attribute for an object and don’t initialize the array during the call of init(), all the modification made to the array like push, unshift will affect further creations of objects.
As i dunno if i’m clear enough, here is an example:

<html>
  <head>
    <script type="text/javascript">
    /*
    insert all the John Resig code here
     */

    var Person = Class.extend({
      arrayTest:[],

      init: function(){
      },
      arrayModif: function(){
        this.arrayTest.push(4);
 this.arrayTest.push(2);
      }
    });

    function example(){
      var a=new Person();
      document.write("This is a before any modifications:"+a.arrayTest+"<br/>");
      a.arrayModif();
      document.write("This is a after modifications:"+a.arrayTest+"<br/>");
      var b=new Person();
      document.write("This is b after creation:"+b.arrayTest+"<br/>");
    };
   </script>
  </head>
  <body onload="example();">
  </body>
 </html>

And it will have the following output:

This is a before any modifications:
This is a after modifications:4,2
This is b after creation:4,2

I was wondering if someone had any idea of how to modify John Resig code to achieve the following output, without putting something in init():

This is a before any modifications:
This is a after modifications:4,2
This is b after creation:

Thanks.

  • 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-24T20:05:15+00:00Added an answer on May 24, 2026 at 8:05 pm

    I use a small addition that checks for arrays in the class (not in sub-objects of that class!), and copies those arrays on initialization to get a local copy instead of one in the prototype only.

    var _arrays = [];
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // ... Existing code ...
    
      if (prototype[name] instanceof Array) {
        _arrays.push(name);
      }
    }
    
    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing ) {
        for (var i = 0; i < _arrays.length; i++) {
          this[_arrays[i]] = this[_arrays[i]].slice(0);
        }
        if ( this.init ) {
          this.init.apply(this, arguments);
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.