How do i solve this javascript problem?
const print = console.log;
let animal = {
arr: [1, 2, 3],
};
function Rabbit(name) {
this.name = name;
}
Rabbit.prototype = animal;
let rabbit = new Rabbit("rabbit1");
let rabbit2 = new Rabbit("rabbi2");
print(rabbit.arr);
print(rabbit2.arr);
rabbit.arr.push(4);
print(rabbit.arr);
print(rabbit2.arr);
>[ 1, 2, 3 ]
>[ 1, 2, 3 ]
>[ 1, 2, 3, 4 ]
>[ 1, 2, 3, 4 ]
I'm new to javascript but i know that arr is a reference right so it sets the prototype to a reference to the animal object that holds the reference to the array, so how do you solve this?
Anonymous
9/15/2025, 6:41:22 PM
No.106595278
>>106595306
>>106595888
what the hell const print = console.log;
who the fuck does this, the first time I see it lmao
Anonymous
9/15/2025, 6:44:58 PM
No.106595305
>>106595322
>>106595335
>>106595244 (OP)
>const print = console.log;
High IQ take.
>How do i solve this javascript problem?
Like this:
function Rabbit(name) {
this.name = name;
this.arr = [];
}
Otherwise you're just overwriting data on the prototype instance as you can clearly see.
Anonymous
9/15/2025, 6:45:08 PM
No.106595306
>>106595278
i just like it like that, i type less... can you answer the question?
Anonymous
9/15/2025, 6:47:27 PM
No.106595335
>>106595466
>>106595848
>>106595305
i see, so there isnt a way to naturally make the prototypal object be a new one? I guess that makes sense for memory usage and speed by not creating pointless duplicates
All this prototype stuff is a bit weird to wrap my head around
>>106595322
but i havent gotten to classes yet so i guess that'll do.. Is this how people do it in actual projects? (by people i mean non retards)
Anonymous
9/15/2025, 6:58:20 PM
No.106595466
>>106595573
>>106595335
>so there isnt a way to naturally make the prototypal object be a new one? I guess that makes sense for memory usage and speed by not creating pointless duplicates
The whole idea of prototype chains is that if a field doesn't exist on an instance, it's looked up on the prototype (recursively), for both reading and writing. This gives the intuitive results for "inheriting" methods (it's read from the prototype) and also overriding them (explicitly setting the method's field on an instance avoids the prototype lookup). It also works out when a field has an immutable or primitive value, because trying to set that value writes to the field on the instance. What breaks is when the field refers to some mutable object and you try to modify that object rather than the field itself.
Anonymous
9/15/2025, 7:06:25 PM
No.106595573
>>106595617
>>106595668
>>106595466
so if i wanted to have a copy of the array on each instance, or whenever i have an "fields" as you call it that holds a reference to a mutable object? then i have to manually make sure i clone it or deep copy, i think i remember reading the best way would be to use JSON.stringify and the equivalent decode
function Rabbit(name) {
this.name = name;
this.arr = JSON.parse(JSON.stringify(this.__proto__.arr));
}
correct?
Anonymous
9/15/2025, 7:11:00 PM
No.106595617
>>106595808
>>106595573
You don't gain anything from this. Just take the code you used to initialize the relevant data on the prototype and put it in the instance constructor.
Anonymous
9/15/2025, 7:18:31 PM
No.106595668
>>106595808
>>106596116
>>106595573
For deep copies there's structuredClone().
Anonymous
9/15/2025, 7:30:03 PM
No.106595765
>>106596767
ChatGPT will help you way more than these retards.
Anonymous
9/15/2025, 7:31:21 PM
No.106595776
>ChatGPT will help you way more than these retards.
Why is xe like this?
Anonymous
9/15/2025, 7:34:28 PM
No.106595808
>>106595828
>>106595851
>>106595617
>>106595668
i'm asking whats the convention people use for this kind of things?
also why would i copy whats in the prototype? what if the prototype changes at runtime based on initialized data etc...
Anonymous
9/15/2025, 7:38:46 PM
No.106595848
>>106595828
>>106595335
>but i havent gotten to classes yet so i guess that'll do..
how did people did it before classes which are a recent addition?
Anonymous
9/15/2025, 7:39:01 PM
No.106595851
>>106595884
>>106595808
I don't know what you're on about now. I already told you what the correct thing to do is:
function Rabbit(name) {
this.name = name;
this.arr = [1, 2, 3];
}
> what if the prototype changes at runtime based on initialized data etc...
Then you're most likely abusing prototypes. If the initialization of 'arr' depends on runtime conditions, just pass the value into the constructor. Simple as.
Anonymous
9/15/2025, 7:41:55 PM
No.106595884
>>106595950
>>106595851
so let's say i have 50 functions that all inherit from animal, should i be retarded and add in each function constructor the computation to initialize arr or i can just add it in 1 place in the prototype and then clone it in the function?
Don't take my stupidity for malice, i'm just trying to understand the common conventions for these things, am i overthinking it?
Anonymous
9/15/2025, 7:42:24 PM
No.106595888
>>106595278
Me too, Iām gonna start using it
Anonymous
9/15/2025, 7:49:44 PM
No.106595950
>>106595884
>so let's say i have 50 functions that all inherit from animal
And they all initialize arr in the same way? Ok, as you can see, the constructor is actually just a function. You can execute it on the 'this' object inside another constructor:
function GayRabbit(name) {
Rabbit.call(this, name);
this.color = "rainbow";
}
Anonymous
9/15/2025, 8:03:04 PM
No.106596058
>>106595244 (OP)
What are you trying to do here?
Anonymous
9/15/2025, 8:08:29 PM
No.106596107
>>106595244 (OP)
>retard doesn't understand what a pointer is.
all objects are effectively pointers to mutable memory in javascript. you're sharing a mutable state.
Anonymous
9/15/2025, 8:09:29 PM
No.106596116
>>106595668
last time I bothered, I found JSON.* methods to be faster.
Anonymous
9/15/2025, 8:30:35 PM
No.106596318
>>106596391
You use prototypes to define shared resources, like functions. If you want property to be unique per instance, put in the constructor. If that's too confusing, use class syntax. They work the same way (cause it's just a syntactic sugar), but are more clear and comfy. Classes in JS even have private fields now.
I hope JS is not your first programing language.
Anonymous
9/15/2025, 8:42:56 PM
No.106596415
>>106596442
>>106596391
I'm posting with a cock in my mouth right now. They said, we can't whistle. They said nothing about typing.
Anonymous
9/15/2025, 8:46:04 PM
No.106596442
>>106596415
>I'm posting with a cock in my mouth right now
Very classy. Stay in your comfort zone.
>>106595244 (OP)
Just use classes. Many professional JS/TS programmers wouldn't really be able to answer questions about prototypes because we all just use class and have done for many years.
Anonymous
9/15/2025, 9:01:54 PM
No.106596611
>>106596667
>>106596449
>Many professional JS/TS programmers wouldn't really be able to answer questions about prototypes
Imagine getting filtered by prototypes. Not surprised. Webshits are hopeless.
Anonymous
9/15/2025, 9:08:21 PM
No.106596667
>>106596726
>>106596849
>>106596611
I once knew how they worked, but that was... maybe 10 to 15 years ago? Since then they've been completely irrelevant to my work and to my personal projects.
I could look it up in a few minutes probably but my point is that someone "new to javascript" like OP shouldn't be wasting time and energy on this. It's basically trivia, not something you need to know to write JS.
Anonymous
9/15/2025, 9:12:47 PM
No.106596709
>>106596696
>writing console.log over and over
>using a class instead of a prototype
saaaaaar!! you need to extend the class, saar!
Anonymous
9/15/2025, 9:15:21 PM
No.106596726
>>106596667
You basically just confirmed my point that webshits are all retarded. I hardly ever use JS at all. I read about prototypes once, which was a good 20 years ago, and simply grasped the concept.
Anonymous
9/15/2025, 9:18:20 PM
No.106596756
no clue, ask the clanker ChatGPT
Anonymous
9/15/2025, 9:22:12 PM
No.106596781
>>106596815
>>106596849
>>106596449
you should use classes because inheritance bullshit is easier. there is zero reason for prototypal OO in 2025 nor should users deal with it.
Anonymous
9/15/2025, 9:26:02 PM
No.106596815
>>106596856
>>106596781
>there is zero reason for prototypal OO in 2025
There is zero reason for any OO in 2025, but if you're going to use it, at least use the flexible version that lets you do cool things and keeps every element as a first-class object.
Anonymous
9/15/2025, 9:29:56 PM
No.106596849
>>106596899
>>106596449
>>106596667
>>106596781
Daily reminder that if you ever override a method on an instance in your JS code, you're doing prototypal OOP. Yes. Yes, you are. You don't understand what you're doing (natural for webshits) but it's still what happens.
Anonymous
9/15/2025, 9:30:19 PM
No.106596856
>>106596865
>>106596884
>>106596815
this is javascript moron. you can't walk away from it.
Anonymous
9/15/2025, 9:31:44 PM
No.106596865
>>106596884
>>106596856
>b-b-but this is heckin' javascript!!!!!
So what? Just write your code in a procedural or functional style.
Anonymous
9/15/2025, 9:33:40 PM
No.106596883
>>106596928
>>106595244 (OP)
class Rabbit {
name;
arr = [1, 2, 3];
constructor(name) {
this.name = name;
}
}
let rabbit = new Rabbit("rabbit1");
let rabbit2 = new Rabbit("rabbi2");
console.log(rabbit.arr);
console.log(rabbit2.arr);
rabbit.arr.push(4);
console.log(rabbit.arr);
console.log(rabbit2.arr);
>[ 1, 2, 3 ]
>[ 1, 2, 3 ]
>[ 1, 2, 3, 4 ]
>[ 1, 2, 3 ]
Anonymous
9/15/2025, 9:33:41 PM
No.106596884
>>106596856
>>106596865
ya'll should use Scratch instead. that's the shit
Anonymous
9/15/2025, 9:35:15 PM
No.106596899
>>106596849
the difference is you can't just shit up your prototype and they cry that your shitty Array.prototype.flatten() would break if the standards committee takes it for their own to implement.
>>106596883
oh wow, that's exactly what i asked, if there's a way to do it by default and everybody said there isnt... the absolute state of this board is insane, fucking retards
thank you
Anonymous
9/15/2025, 9:46:09 PM
No.106596992
>>106596928
I just don't think anyone realized how little you know and assumed you explicitly want to use prototypes for some reason.
Multiple people did told you to use classes though.
Anonymous
9/15/2025, 9:48:39 PM
No.106597026
>>106597055
>>106596928
>oh wow, that's exactly what i asked, if there's a way to do it by default and everybody said there isnt... the absolute state of this board is insane, fucking retards
You're the retard. He just used the class syntax to do the exact thing I showed you how to do using prototypes as you requested.
Anonymous
9/15/2025, 9:50:32 PM
No.106597045
>>106597127
Yeah i didnt get to read about classes syntax but now that i remember my question still stands
If i have a computationally expensive initialization that i want to do, to then be used in all instances of a class, what's the convention to go about it?
And yes, i am retarded
Anonymous
9/15/2025, 9:51:21 PM
No.106597055
>>106597151
>>106597026
they stole your achievement man. that's tough
Anonymous
9/15/2025, 9:57:20 PM
No.106597117
>>106597127
>>106597142
>>106596928
const heavy = () => [1, 2, 3];
class Rabbit {
static STATIC = heavy();
name;
constructor(name) {
this.name = name;
}
}
let rabbit = new Rabbit("rabbit1");
let rabbit2 = new Rabbit("rabbi2");
console.log(rabbit.name);
console.log(Rabbit.STATIC);
Anonymous
9/15/2025, 9:59:52 PM
No.106597142
>>106597117
ah ok, seems like im missing a lot of stuff, i'll keep on trucking but thanks for explaining
Anonymous
9/15/2025, 10:01:08 PM
No.106597151
>>106597198
>>106597412
>>106597055
You sound like a complete faggot. When someone calls me a retard for providing the correct answer to the question asked, I think some clarification is in order regarding who the actual retard is.
Anonymous
9/15/2025, 10:04:42 PM
No.106597198
>>106597151
hope i can win the retard competition
Anonymous
9/15/2025, 10:25:58 PM
No.106597412
>>106597461
>>106597151
btw, you sound like a fucking nerd
Anonymous
9/15/2025, 10:31:56 PM
No.106597461
>>106597477
>>106597412
>btw, you sound like a fucking nerd
Yeah. So? Go smoke a cock, normalfaggot.
Anonymous
9/15/2025, 10:33:30 PM
No.106597477
>>106597514
>>106597461
why did you add normal to the insult? is there a shiny faggot, like pokemon?
Anonymous
9/15/2025, 10:38:14 PM
No.106597514
>>106597528
>>106597477
"Normal" is the insult part, normie.
Anonymous
9/15/2025, 10:43:10 PM
No.106597560
>>106597549
fuck yeah man, got any discord or smth?