[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: 1726575441784444.png (213 KB, 716x641)
213 KB
213 KB PNG
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?
>>
;


const me guess
>>
>>106595255
what?
>>
what the hell
const print = console.log;

who the fuck does this, the first time I see it lmao
>>
>>106595244
>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.
>>
>>106595278
i just like it like that, i type less... can you answer the question?
>>
>>106595305
>overwriting
I mean modifying*.
>>
>>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)
>>
>>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.
>>
>>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?
>>
>>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.
>>
>>106595573
For deep copies there's structuredClone().
>>
ChatGPT will help you way more than these retards.
>>
File: redditsoi.png (33 KB, 584x841)
33 KB
33 KB PNG
>ChatGPT will help you way more than these retards.
Why is xe like this?
>>
>>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...
>>
>>106595808
Just use a class broski
>>
>>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?
>>
>>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.
>>
>>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?
>>
>>106595278
Me too, I’m gonna start using it
>>
>>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";
}
>>
File: 1757059924361.jpg (39 KB, 400x399)
39 KB
39 KB JPG
>>106595244
What are you trying to do here?
>>
>>106595244
>retard doesn't understand what a pointer is.
all objects are effectively pointers to mutable memory in javascript. you're sharing a mutable state.
>>
>>106595668
last time I bothered, I found JSON.* methods to be faster.
>>
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.
>>
>>106596318
>""""""""""classes""""""""""" are more comfy
Jesus, what a faggot.
>>
>>106596391
I'm posting with a cock in my mouth right now. They said, we can't whistle. They said nothing about typing.
>>
>>106596415
>I'm posting with a cock in my mouth right now
Very classy. Stay in your comfort zone.
>>
>>106595244
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.
>>
>>106596449
grim
>>
>>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.
>>
>>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.
>>
>>106595244
>aliasing console.log
>using a prototype instead of a class
saar
>>
>>106596696
>writing console.log over and over
>using a class instead of a prototype
saaaaaar!! you need to extend the class, saar!
>>
>>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.
>>
no clue, ask the clanker ChatGPT
>>
>>106595765
word
>>
>>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.
>>
>>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.
>>
>>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.
>>
>>106596815
this is javascript moron. you can't walk away from it.
>>
>>106596856
>b-b-but this is heckin' javascript!!!!!
So what? Just write your code in a procedural or functional style.
>>
>>106595244
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 ]
>>
>>106596856
>>106596865
ya'll should use Scratch instead. that's the shit
>>
>>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
>>
>>106596928
no problem man, anytime
>>
>>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.
>>
>>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.
>>
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
>>
File: 1268.png (769 KB, 1200x1200)
769 KB
769 KB PNG
>>106597026
they stole your achievement man. that's tough
>>
>>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);
>>
>>106597117
Was meant for >>106597045
>>
File: 1733531706672750.png (455 KB, 675x611)
455 KB
455 KB PNG
>>106597117
ah ok, seems like im missing a lot of stuff, i'll keep on trucking but thanks for explaining
>>
>>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.
>>
>>106597151
hope i can win the retard competition
>>
>>106597151
btw, you sound like a fucking nerd
>>
>>106597412
>btw, you sound like a fucking nerd
Yeah. So? Go smoke a cock, normalfaggot.
>>
>>106597461
why did you add normal to the insult? is there a shiny faggot, like pokemon?
>>
>>106597477
"Normal" is the insult part, normie.
>>
>>106597514
can we be friends?
>>
>>106597528
Of course, newfriend.
>>
>>106597549
fuck yeah man, got any discord or smth?
>>
>>106595244
>rabbi
sure, goyim
>>
>>106597045
Use the singleton pattern?



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.