← Home ← Back to /g/

Thread 106595244

63 posts 12 images /g/
Anonymous No.106595244 >>106595305 >>106596058 >>106596107 >>106596449 >>106596696 >>106596883
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 No.106595255 >>106595265
;


const me guess
Anonymous No.106595265
>>106595255
what?
Anonymous No.106595278 >>106595306 >>106595888
what the hell const print = console.log;
who the fuck does this, the first time I see it lmao
Anonymous 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 No.106595306
>>106595278
i just like it like that, i type less... can you answer the question?
Anonymous No.106595322 >>106595335
>>106595305
>overwriting
I mean modifying*.
Anonymous 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 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 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 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 No.106595668 >>106595808 >>106596116
>>106595573
For deep copies there's structuredClone().
Anonymous No.106595765 >>106596767
ChatGPT will help you way more than these retards.
Anonymous No.106595776
>ChatGPT will help you way more than these retards.
Why is xe like this?
Anonymous 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 No.106595828 >>106595848
>>106595808
Just use a class broski
Anonymous 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 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 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 No.106595888
>>106595278
Me too, I’m gonna start using it
Anonymous 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 No.106596058
>>106595244 (OP)
What are you trying to do here?
Anonymous 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 No.106596116
>>106595668
last time I bothered, I found JSON.* methods to be faster.
Anonymous 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 No.106596391 >>106596415
>>106596318
>""""""""""classes""""""""""" are more comfy
Jesus, what a faggot.
Anonymous 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 No.106596442
>>106596415
>I'm posting with a cock in my mouth right now
Very classy. Stay in your comfort zone.
Anonymous No.106596449 >>106596518 >>106596611 >>106596781 >>106596849
>>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 No.106596518
>>106596449
grim
Anonymous 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 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 No.106596696 >>106596709
>>106595244 (OP)
>aliasing console.log
>using a prototype instead of a class
saar
Anonymous 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 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 No.106596756
no clue, ask the clanker ChatGPT
Anonymous No.106596767
>>106595765
word
Anonymous 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 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 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 No.106596856 >>106596865 >>106596884
>>106596815
this is javascript moron. you can't walk away from it.
Anonymous No.106596865 >>106596884
>>106596856
>b-b-but this is heckin' javascript!!!!!
So what? Just write your code in a procedural or functional style.
Anonymous 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 No.106596884
>>106596856
>>106596865
ya'll should use Scratch instead. that's the shit
Anonymous 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.
Anonymous No.106596928 >>106596969 >>106596992 >>106597026 >>106597117
>>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 No.106596969
>>106596928
no problem man, anytime
Anonymous 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 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 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 No.106597055 >>106597151
>>106597026
they stole your achievement man. that's tough
Anonymous 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 No.106597127
>>106597117
Was meant for >>106597045
Anonymous No.106597142
>>106597117
ah ok, seems like im missing a lot of stuff, i'll keep on trucking but thanks for explaining
Anonymous 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 No.106597198
>>106597151
hope i can win the retard competition
Anonymous No.106597412 >>106597461
>>106597151
btw, you sound like a fucking nerd
Anonymous No.106597461 >>106597477
>>106597412
>btw, you sound like a fucking nerd
Yeah. So? Go smoke a cock, normalfaggot.
Anonymous No.106597477 >>106597514
>>106597461
why did you add normal to the insult? is there a shiny faggot, like pokemon?
Anonymous No.106597514 >>106597528
>>106597477
"Normal" is the insult part, normie.
Anonymous No.106597528 >>106597549
>>106597514
can we be friends?
Anonymous No.106597549 >>106597560
>>106597528
Of course, newfriend.
Anonymous No.106597560
>>106597549
fuck yeah man, got any discord or smth?