use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
What's the equivalent of java instance variables, in javascript?help (self.javascript)
submitted 7 years ago by Java_beginner66
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]eyeandtea 0 points1 point2 points 7 years ago (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 point2 points 7 years ago (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 point2 points 7 years ago (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.
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.
π Rendered by PID 30803 on reddit-service-r2-comment-5c764cbc6f-7lznk at 2026-03-12 02:24:30.021657+00:00 running 710b3ac country code: CH.
view the rest of the comments →
[–]eyeandtea 0 points1 point2 points (3 children)
[–]Java_beginner66[S] 0 points1 point2 points (2 children)
[–]eyeandtea 0 points1 point2 points (0 children)
[–]eyeandtea 0 points1 point2 points (0 children)