Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API

From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
idk if that private does anything
also that's confusing to read
oh how I miss js but not that if() syntax. rust omits () and returns can be made without return keyword also. js dynamic variables tho -
@jestdotty I know. JS also allows the omission of (), {} and return but I rather keep it for clarity, as keeping a multi-line if body in one line can introduce bugs.
-
BTW arrays in js have the includes function for checking membership. Also, why are you re-implementing a Set?
Also the constructor return is convenient if you want to do fancy stuff with proxies for instance.
Also also javascript doesn't actually have classes and what you see as a class is actually just sugar around the prototype system -
@BordedDev I know they do, but I'm just following the book examples. I think the author wants the reader to do these exercises so that they can understand the concepts deeper. And he's right, for example, I do understand the classic Array methods better now by implementing them myself, otherwise it would be difficult to keep memorizing and only surface-level understanding what they do.
Hm, proxies. Yes, I know the concept but it's a bit long ago now. lol. Proxy pattern.
Yes, syntactic sugar. As I learned: there is no OOP in JS, just mimicking. -
js is so great. you can do so much wacky stuff 🤤
*cries in straitjacket rust*
fuck I could make a bot with proxies so easy rn, for literally no reason, but noooo I have to be helpless and stuck in rust instead -
@jestdotty Wacky Cartoons! They're tiny, they're toony, they're all a little looney! jk
Yes, in JS you are freeeeeeeeeee. Partially.. well.. hm. Doesn't TypeScript help limit things? lol.
StraitJacket Java, C#...
So.. you're rusted in your rust. :O
Related Rants
JavaScript keeps surprising me, or I'm just rushing. lol
class Group {
#group;
constructor(){
this.group = [];
return this.group;
}
add(item) {
// only if it isn't already a member
// value can be part of set only once
if(!this.has(item)) {
group.push(item);
}
}
has(item){
// return boolean: item member of group
for(let i=0; i<group.length; i++) {
if(group[i] === item) {
return true;
}
}
return false;
}
let group = new Group();
group.add(1);
console.log(group);
Error: group.add() is not a function
Why is group.add() not a function?
A.I. analysis:
1. Private field vs public field:
A private field #group can only be accessed as this.#group. But in the constructor, I wrote:
this.group = [];
This creates a new public field, unrelated to #group.
2. Returning from constructor:
return this.group;
In JavaScript, a constructor automatically returns this, which is the instance of the class. Returning this.group replaces the instance with the array. That’s why after new Group(), your variable group is just an array, not an instance of Group. Arrays don’t have the add method — hence the error.
3. Referencing group inside methods:
group.push(item);
Here group is not defined. You probably meant this.group (or this.#group if using the private field).
Note to self: in the magic of computer science, you must be precise. If I loosely define the black box, the result will have a chance of producing anomalies.. kind of like reality. lol
rant
javascript
not-a-function
classes
error