0

I have a prototype with 3 methods in it as below,

ej.Ej3DRender = function () {


};
 ej.Ej3DRender.prototype = {

        matrix3D: function (size) {          

                mData += 3;
         },
        getIdentity: function () {
             mData+=4;
        },
        turn: function (angle) {
             mData+=6;
        }
    }

Is it possible to share a variable common to all the methods in the prototype? In the above code i have to make use of the "mData" in all method, so where can i declare it? and it should be accessible only within this prototype.

Thanks In Advance

user3326265
  • 257
  • 1
  • 4
  • 14

2 Answers2

0

The short answer is - No you can't do it in right way.

The long answer:

You can declare the variable as private with var keyword in funciton constructor but the other functions must be declared in the

ej.Ej3DRender = function () {
    var mData = 0;

    this.matrix3D = function(size) { mData += 3 };
    // .. other functions that access mData
};

Note that this is considered as very bad practice cause the functions are created and attached for every newly created object.

In JavasScript is accepted to prefix private members with underscore '_'. You can try this aproach (which won't have the performance issues with creating functions every time, the variable will be visible 'inside' AND 'outside' the prototype but while it's prefixed with underscore the developer shouldn't touch the variable from 'outside') :

ej.Ej3DRender = function () {
    this._mData = 0;
};

ej.Ej3DRender.prototype = {
    matrix3D: function (size) {          
        this._mData += 3;
    },
    getIdentity: function () {
        this._mData += 4;
    },
    // ... other functions
}
Viktor Bahtev
  • 4,858
  • 2
  • 31
  • 40
  • Your answer is wrong, it is possible to do this. I'll add an answer. Note that the OP needs a SHARED variable – HMR Jul 24 '14 at 09:03
  • I am pretty sure that OP wants shared variable between methods of concrete instance not shared between all instances. However the question is not clear enough and need edit from OP. – Viktor Bahtev Jul 24 '14 at 10:19
  • Could be, shared on prototype usually means shared by instances but maybe the OP doesn't know that. For multiple private instance specific it is possible to create only one closure that locks the private members in a box. A pattern similar as the protected pattern in my answer. I don't like private much since it could negatively affect testing and monkey patching – HMR Jul 24 '14 at 12:03
0

Is it possible to share a variable common to all the methods in the prototype?

Yes it's possible. If the value is shared (not instance specific) and you need it to be private you can wrap the functions needing them in a IIFE

var Test = function(){};
Test.prototype.notHavingTheVar=function(){
  console.log(typeof sharedPrivate);
}

//IIFE
;(function(){
  var sharedPrivate=22;
  Test.prototype.onePrivilegedFn=function(){
    console.log(++sharedPrivate);
  };
  Test.prototype.twoPrivilegedFn=function(){
    console.log(++sharedPrivate);
  };
}());
var t = new Test();
t.notHavingTheVar();//=undefined
t.onePrivilegedFn();//=22
t.twoPrivilegedFn();//=23
//it is shared so not instance specific
var t2 = new Test();
t2.twoPrivilegedFn();//=24 <<<====please note, it's shared

More about constructor functions, prototype and a pattern for protected instance specific members can be found here.

Private instance specific members can't be accessed from prototype.

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