there was motions to try and make "records" or whatever in javascript, but it's probably in committee hell still.
the bigger problem is you can't really use objects as keys, even with Map, it's based on the underlying location in memory, not the actual value of.
honestly all they need to do is add something like toString for == / ===
>using plain javascript in 2024
ngmi
>default any prevents you from writing into a type that doesn't have .a property
ok.
You are not changing out the object the variable is pointing to. You can change the object itself, but x will always point to it and not another object.
Only a no-coder that has never touched a real typed language could cope like this. Writing anything more complex than fizzbuzz in plain Javascript is just a bug-ridden disaster.
The actual solution to the Javascript problem is not using Javascript, though. But it is unavoidable in some situations unfortunately.
commented version for idiots:
int main() {
const char * x = malloc(16);
char * p = (char *) x; // tell the compiler we dont care
strcpy(p, "lol");
strcpy(p, "lmao");
printf("%sn", p); // everything works
}
i still need someone to translate this to ebonics thoughever
its completely normal, only zoomers with a superficial understanding of programming cant understand
total cargo cultist death
>i have some data >i point to it with a big red arrow >my big red arrow is constant and can't change >the data that my big red arrow points to can change
is it really so complicated?
>thank god you can still earn 6 figures without ever bothering learning stupid pointers
holy shit the market hasn't corrected enough yet if this is still true
all complex datatypes (arrays / objects) are basically pointers. Symbols are weird because their "value" is more of a description and sourcing global symbols is just weird imo.
>but we can solve it with just two cope classes and three cope dependency injections and like ten constructors and also type eight times, bro, it's very easy!
why is it that all languages have to use the most complicated constructs to force things to work?
apparently const x = const {} is too damn intuitive and that's le bad
yeah consistent syntax would have been le bad, let's make const work only on lvalues
great language design bro, just use Const.Constinator.ConstInit() bro
3 weeks ago
Anonymous
It's certainly more consistent than having const sometimes mean const, and other times mean immutable, like what you're asking for.
commented version for idiots:
int main() {
const char * x = malloc(16);
char * p = (char *) x; // tell the compiler we dont care
strcpy(p, "lol");
strcpy(p, "lmao");
printf("%sn", p); // everything works
}
i still need someone to translate this to ebonics thoughever
It's impressive how 1st year kids make moronic threads like this as if they found something clever when in reality they just make a fool of themselves.
if you think this is somehow incoherent or nonsensical you're the biggest nocoder on IQfy and should be ashamed of shitting up the board like this
const in JavaScript, and more broadly immutability in all programming languages, refers to the binding. in any language in existence you can do what's in the OP, what's not allowed is reassigning the variable itself, that's the whole point.
>and more broadly immutability in all programming languages, refers to the binding.
Nah, in C/C++ for example, const is part of the type itself.
struct foo_t { int x; };
int main() {
printf("%in", bar().x);
}
Not that returning a const value is useful or anything. BUT, this property of const is why you can have things like const int*, where the binding is mutable but the value being pointed to isn't.
>I don't grok the idea of binding an object to a name, and so I don't grok anything that follows from it.
It's still dumb
you being a dumb moron making a thread complaining about something you don't understand doesn't make it dumb, it makes you dumb
Ok make the "a" member const, smartypants
there was motions to try and make "records" or whatever in javascript, but it's probably in committee hell still.
the bigger problem is you can't really use objects as keys, even with Map, it's based on the underlying location in memory, not the actual value of.
honestly all they need to do is add something like toString for == / ===
>default any prevents you from writing into a type that doesn't have .a property
ok.
https://github.com/tc39/proposal-record-tuple
kek, still stage 2. I still kek having to serialize and deserialize objects just to have proper value compare. meme language.
Why do sóydevs keep trying to make javascript a stricter language? there are trillions of languages out there that are strict, literally go use them
js for frontend
js for backend
js everywhere
>grok
>grok
back to hacker reddits please
Isn't const for variables, not for objects?
If x was read only you could not define .a at all.
You are not changing out the object the variable is pointing to. You can change the object itself, but x will always point to it and not another object.
const x = {'a': 'moron'};
>writable: false
Done t. Soiscript guru
it's a const pointer.
it's not like jskiddies know anything about pointers.
"const pointer" is ambiguous
could be either
Object const*
or
Object* const
which mean different things (behavior in js is analogous to second case)
No its not, the linguistic distinction is "constant pointer" and "pointer to a constant".
Are you actually looking for logic and sense in JS? just finish your client side code and be done with it. Questioning will only shorten your life
>using plain javascript in 2024
ngmi
>"compiling" a language with fake types into a language with no types
ngmi
Only a no-coder that has never touched a real typed language could cope like this. Writing anything more complex than fizzbuzz in plain Javascript is just a bug-ridden disaster.
The actual solution to the Javascript problem is not using Javascript, though. But it is unavoidable in some situations unfortunately.
I love Java and C# and types. But front end typescript is just unnecessary pain in ass especially when importing plain javascript libraries.
JavaScript is the supreme programming language. Real typed languages are discriminatory. And TypeScript is just an abomination with no merits at all.
ts+lua is very comfy
transsexuals
by that logic 99% of all types in large C/C++ codebases are not real types
Those are just type errors, though.
ts is pretty much just fake js.
Is there a good, short, introduction to javascript for people that know some other language (C, python) ?
This is as good of an intro as you'll get
https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/README.md
ty
>constant
>not actually a constant
I'm not JS dev or anything, but isn't there some other weird behaviours with strings or whatever?
yes but const isnt weird here:
its completely normal, only zoomers with a superficial understanding of programming cant understand
total cargo cultist death
It is constant though.
If you instead want an /immutable/ object, you use Object.freeze.
give one good reason why anything shouldn't be mutable
Long story short because locking variables ensures API boundary safety
compiler optimizations
>IQfy gets filtered by pointers again
pointers are fricking moronic anyway
thank god you can still earn 6 figures without ever bothering learning stupid pointers
>i have some data
>i point to it with a big red arrow
>my big red arrow is constant and can't change
>the data that my big red arrow points to can change
is it really so complicated?
>thank god you can still earn 6 figures without ever bothering learning stupid pointers
holy shit the market hasn't corrected enough yet if this is still true
>no * anywhere
>pointers
wow it's as obscure as in shitty cnile libraries where they typedef a pointer
all complex datatypes (arrays / objects) are basically pointers. Symbols are weird because their "value" is more of a description and sourcing global symbols is just weird imo.
I just made an explanation in Swift
>but we can solve it with just two cope classes and three cope dependency injections and like ten constructors and also type eight times, bro, it's very easy!
why is it that all languages have to use the most complicated constructs to force things to work?
apparently const x = const {} is too damn intuitive and that's le bad
Object.freeze already exists and has none of those problems.
yeah consistent syntax would have been le bad, let's make const work only on lvalues
great language design bro, just use Const.Constinator.ConstInit() bro
It's certainly more consistent than having const sometimes mean const, and other times mean immutable, like what you're asking for.
in which world are the two concepts not the same?
>x always points to the same object
>you can still add and edit keys/values
truly a mystery
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
const char * x = malloc(16);
char * p = (char *) x;
memset(p, 0, sizeof p);
strcpy(p, "lol");
strcpy(p, "lmao");
printf("%sn", p);
return 0;
}
commented version for idiots:
int main() {
const char * x = malloc(16);
char * p = (char *) x; // tell the compiler we dont care
strcpy(p, "lol");
strcpy(p, "lmao");
printf("%sn", p); // everything works
}
i still need someone to translate this to ebonics thoughever
It's impressive how 1st year kids make moronic threads like this as if they found something clever when in reality they just make a fool of themselves.
constant reference to an object with mutable members 🙂
const ={};
x.a="homie";
y={};
y.a="digga";
x=y;
console.log(x.a);
ERROR!
x=y;
^
TypeError: Assignment to constant variable.
>Yawascript
Trash lang
>const is actually a constant reference
>object and arrays are reference based
makes sense homosexual
>this board is plagues by morons like this
what you're looking for is let x = Object.freeze({a: 'lol'});.
>x = {}
You would use const to prevent x from being reassigned, but otherwise yes this is the answer.
Websitters will jump to the defense of this because of sunk cost. Needfulscript has to die.
>not understanding references, primitive values and how const works in JS
You have no idea how many people have been filtered to this where I work.
>variables are mutable by default
Absolutely disgusting
>variables are mutable
It's in the name.
Const means the binding happens once at assignment, and cannot be changed. It doesn't mean everything is immutable.
The name is constant
HAHAHAHAHAHAHAHAHA
Js troons btfo !
The only thing const in JS does is prevents something else to be assigned the same name.
Works on my machine
if you think this is somehow incoherent or nonsensical you're the biggest nocoder on IQfy and should be ashamed of shitting up the board like this
const in JavaScript, and more broadly immutability in all programming languages, refers to the binding. in any language in existence you can do what's in the OP, what's not allowed is reassigning the variable itself, that's the whole point.
>and more broadly immutability in all programming languages, refers to the binding.
Nah, in C/C++ for example, const is part of the type itself.
struct foo_t { int x; };
const struct foo_t bar() {
struct foo_t baz = { 1 };
return baz;
}
int main() {
printf("%in", bar().x);
}
Not that returning a const value is useful or anything. BUT, this property of const is why you can have things like const int*, where the binding is mutable but the value being pointed to isn't.
I'm not convinced most professional JS devs understand how const and let are supposed to be used
idk I don't understand meme languages
I lust use let for everything. isn't a const in JavaScript mutable?