0

I have declared object 'Render' and added 2 object 'vector3D'& 'matrix' to it through the prototype. For vector3D object, i have included 3 property through its prototype.

Render = function () {    

};


  Render.prototype = {
        vector3D: function () {

        },     
         matrix: function () {

        },   
   }

   var Render = new Render();
   Render.vector3D.prototype = {
        //Members
        x: "",
        y: "",
        z: "",


        isValid: !isNaN(this.x) && !isNaN(this.y) && !isNaN(this.z),
  }

If i want to access the vector properties, i have to follow the below steps each time

var Rendered = new Render();
var vector=new Rendered.vector3D();
vector.x=10;
vector.y=20;

Is this correct or is there any other easy way to access the properties of vector object?

Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79
user3326265
  • 257
  • 1
  • 4
  • 14
  • possible duplicate of [How does JavaScript .prototype work?](http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work) – Umur Kontacı Jul 28 '14 at 05:56

1 Answers1

0

Why not do something like this:

Render.prototype = {
        vector3D:  {
                    x: "",
                    y: "",
                    z: "",
        },     
         matrix:  {

        },   
   }

That is, just declare vector3D as object not function.

You access it like this:

var rendered = new Render();
rendered.vector3D.x = 10;

However, if you do it like this then all your Render instances will share the same value for vector coordinates because you set them on the prototype. If you want a new instance of the vector to get initialized each time you need to declare it an "instance" variable:

Render = function () {
    this.vector3D ={
                    x: "dd",
                    y: "",
                    z: "",
        }
};
Liviu Mandras
  • 6,540
  • 2
  • 41
  • 65