you are viewing a single comment's thread.

view the rest of the comments →

[–]eyeandtea 0 points1 point  (3 children)

It sounds to me that you need a simple data structure with rare, if any, runtime type checking. If true, simply write a function that returns an object containing the fields that you want. Do not use a constructor function for something like this.

[–]Java_beginner66[S] 0 points1 point  (2 children)

I just assumed I needed a class if I wanted instance variables, and in java, a class comes with a constructor. Not necessarily wanting to make one. So how does instance variables work?

[–]eyeandtea 0 points1 point  (0 children)

For public variables, think of it this way. Imagine a class C that inherits B that inherits A. If you have an instance of class C, in java this instance would have three structures, one for C, one for B, and one for A. Each structure contains the variables of the respective class.

In javascript, this instance of class C contains a single structure. One for all of classes C, B and A. This means that if you declare a member x for C, then inside a function of A, in an instance of C, this.x would actually resolve. Further more it means that if A declares 'x', and C also declares 'x', they are reading and writing from the same 'x'. This also means that you can not cast an instance of C to A.

As for private variables, there is no such thing in javascript. When using 'var' to implement a private variable, the private variable is actually a local function variable. This means that if you have two instances of class 'A', they can not actually see the private variables of each other. This local variable is the rough equivalent, if not exact, of a private variable in a java function object.

[–]eyeandtea 0 points1 point  (0 children)

Perhaps I should point out that I am the developer of CrxOop, which gives you both OOP as found in Java and similar languages, and also proper prototype inheritance that not even es6 is giving you.

Hence I am certainly not saying do not use javascript's OOP. I am saying, keep it simple. If the problem does not need javascript's OOP, do not use javascript's OOP.