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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:36:09+00:00 2026-05-27T01:36:09+00:00

I defined my classes like that: function Employee () { this.name = ; this.val

  • 0

I defined my classes like that:

function Employee () {
    this.name = "";
    this.val = new Array();
}

function WorkerBee () {
    this.beeQueen = "lola";

    this.setVal = function(val) {
        this.val.push(val);
    };
}
WorkerBee.prototype = new Employee;


function SalesPerson () {
    this.dept = "development";  
}
SalesPerson.prototype = new WorkerBee;

function Engineer () {
    this.dept = "R&D";
}
Engineer.prototype = new WorkerBee;

Problem: all the objects I create share the same val array:

var mark = new WorkerBee;
mark.name = "Mark";
mark.setVal('00000')

var louis = new SalesPerson;
louis.name = "Louis";
louis.setVal('11111')

var ricky = new SalesPerson;
ricky.name = "Ricky";
ricky.setVal('33333')

var john = new Engineer;
john.name = "John";
john.setVal('55555');

This:

html += "<br /><br />Name: " + mark.name;
html += "<br />Val: " + mark.val;

html += "<br /><br />Name: " + louis.name;
html += "<br />Val: " + louis.val;

html += "<br /><br />Name: " + ricky.name;
html += "<br />Val: " + ricky.val;

html += "<br /><br />Name: " + john.name;
html += "<br />Val: " + john.val;

displays:

Name: Mark
Val: 00000,11111,33333,55555

Name: Louis
Val: 00000,11111,33333,55555

Name: Ricky
Val: 00000,11111,33333,55555

Name: John
Val: 00000,11111,33333,55555

I read http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/ and http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/ but I’m still confused!

When I use a string instead of an array this works well (because the reference to the string is overwritten I suppose) but how to do it with array ?

So I can have:

Name: Mark
Val: 00000

Name: Louis
Val: 11111

Name: Ricky
Val: 33333

Name: John
Val: 55555

  • 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-27T01:36:10+00:00Added an answer on May 27, 2026 at 1:36 am

    You need to apply the “parent” constructor in your “inheriting” constructor functions:

    function WorkerBee () {
        Employee.apply(this);
        /*...*/
    }
    WorkerBee.prototype = new Employee();
    

    Do the same for all your “inherited” constructor functions:

    function SalesPerson () {
        WorkerBee.apply(this);
        /*...*/
    }
    SalesPerson.prototype = new WorkerBee();
    
    function Engineer () {
        WorkerBee.apply(this);
        /*...*/
    }
    Engineer.prototype = new WorkerBee();
    

    See the example on jsfiddle.

    And like @austincheney pointed out, JavaScript has no “classes” – only functions (which are objects), constructors (which are functions) and objects.


    JavaScript uses prototypal inheritance. This means that when you try to access a property (or function) of an object which doesn’t exist, it will delegate to the prototype object.

    Consider:

    var isaacNewton = {
        name: 'Isaac Newton'
    };
    
    function Scientist() {}
    Scientist.prototype = isaacNewton;
    
    var neilDeGrasseTyson = new Scientist();
    
    console.log(neilDeGrasseTyson.name);
    
    isaacNewton.name = 'Sir Isaac Newton';
    
    console.log(neilDeGrasseTyson.name);
    

    The output here is:

    Isaac Newton
    Sir Isaac Newton
    

    Object neilDeGrasseTyson hasn’t inherited the name property. It simply doesn’t have one. Since it doesn’t have a property name when we try to access name the neilDeGrasseTyson object will delegate to the prototype object Scientist.prototype, and return the value of Scientist.prototype.name which is isaacNewton.name.

    In your code, objects mark, louis, ricky and john don’t have a property val. All those calls to setVal end up manipulating WorkerBee.prototype.val since none of those objects have their own val property. By applying the Employee constructor to them, you introduce the properties of an Employee to them, so they don’t have to delegate.

    To drive the point home a little more, another solution would have been to put the method setVal in Employee and give each “inheriting” constructor a this.val property: http://jsfiddle.net/FDCXF/1/

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

Sidebar

Related Questions

I have 4 classes, one Database helper and 3 defined like this: abstract class
I have an interface that defines some methods I would like certain classes to
I have some classes in C++ that I would like to expose to Lua.
I am passing user defined classes over sockets. The SendObject code is below. It
I have always wondered why you cannot use locally defined classes as predicates to
In a large Application is there any way to distinguish user-defined classes with built-in
I've some classes defined in a dll file. These are in the form of
I have the following classes defined to do validation: public class DefValidator : IValidate<IDef>
I need to replace one of the pre-defined window classes all across Windows. For
I have seen some C++ classes with a destructor defined as follows: class someClass

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.