1

i'm looking for a way to force all of my objects have it's own instance of object in prototype. This object in prototype has it's own private variables. I don't want to share this objects. What should i do?

new function () {
  "use strict";
  function Controls() { };
  Controls.prototype = new Application.Utils.Map(); //instance of this object is shared between all of new Controls objects. I want have new Map object for each new Controls Object. 
  Application.Stored.UI.Controls = new Controls();
  Application.Stored.UI.Controls2 = new Controls();
}();

Map object looks like this:

function(){
  //private variables with methods
  return {
    func: someFunc,
    ...
  };
};

How to achieve this?

Puchacz
  • 1,987
  • 2
  • 24
  • 38
  • possible duplicate of [Define Private field Members and Inheritance in JAVASCRIPT module pattern](http://stackoverflow.com/questions/12463040/define-private-field-members-and-inheritance-in-javascript-module-pattern) – Bergi Aug 24 '13 at 14:32
  • Why is `Controls` empty? I would recommend to use parasitic inheritance for this. And you use of `new function` is dubious. – Bergi Aug 24 '13 at 14:36

1 Answers1

0

If privateness is more important to you than using prototype (prototype methods cannot access private instance variables) and you're not planning on cloning your instances ("private" instance variables can only be accessed by the instance not by the class as in Java). With inheritance then overriding parent's funcitons will be more complicated but there are ways to do it: (put all "class" code in the constructor function) How to call parent class' method from a subclass in JavaScript so that parent's local variables would be accessible?

I never use "private" instance variables but Bergi's suggestion to use module pattern is a good one to have "private" functions (not unique per instance) since a function can be the same for all instaces.

Added this as an answer instead of a comment because the question shows up as having 0 answers but I think Bergi did a good job answering the question in the comments.

Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160