@wallhackio The JavaScript Planet, a dystopian world where JavaScript is the only programming language ever invented
@vaporeon_ I love JavaScript, so delete this please
@vaporeon_ I love JavaScript, so delete this
please
@vaporeon_ npm install memory-management
@vaporeon_ it was a joke lmao
@wallhackio But can you do any memory management in JavaScript?
const deleteThis = () => {
const temp = { _this: this };
delete temp._this;
};
@aescling @vaporeon_ this will NOT do what it's being promised to do, this is a joke
@aescling @vaporeon_ this
is a joke
@wallhackio @aescling What will it do?
@vaporeon_ @aescling it will create an object with a single property named _this
that has the value of whatever this
was where the arrow function was declared
and then it will delete that property from the object
@wallhackio @aescling So creating the object initializes _this
with a copy of the value of this
, and only that copy gets deleted?
@vaporeon_ @aescling yessir
@wallhackio @vaporeon_ not a copy but a refurence
@wallhackio @vaporeon_ what gets deleted is the refurence
@aescling @wallhackio Is a reference the JavaScript version of a pointer?
@vaporeon_ @wallhackio yeah basically
@aescling @vaporeon_ did you know C++ has both pointers and references :D
@wallhackio @aescling I did! I have a lot of trouble with the concept of references that are not pointers, but as far as I understood, it's just telling the compiler a different name for the same variable? Sort of like EQUIVALENCE
in FORTRAN?
@vaporeon_ @aescling they do function as aliases to an existing object but they still pass by value when given to functions, so they can be used in any situation where a pointer can be used because C++ likes giving you multiple ways to solve every problem
@wallhackio @aescling Is there a meaningful performance difference? I could imagine that maybe the compiler could optimize more when using a reference, because pointers can point anywhere in the memory and references can't?
@vaporeon_ @aescling there's no optimization in using a reference over a pointer, it's a style thing
const o = { k: 1 };
function changeObj(o) {
o.k = 2
};
changeObj(o);
console.log(o); // { k: 2 }
@aescling @wallhackio LOL, is that because the pointer is a constant, but the contents of the object are not? In C terms, what happens here is this, right?
changeObj(struct object *o) {
o->k = 2;
}
main() {
const struct object *o = new_object(1);
changeObj(o);
printf("k: %d\n", o->k);
}
@vaporeon_ @aescling yes. in C everything is passed by value between functions, so technically the pointer is copied from main
to changeObj
but pointers are just numbers so the literal address in memory that pointer refers to doesn't change
@aescling @wallhackio There are two types of languages: those that have explicit pointers and those that hide the concept of a pointer from the user and therefore become very confusing
@aescling @wallhackio Or maybe my mental model of JavaScript etc. is just insufficient and I just need to read the correct documentation about it?
But honestly? In C, this is completely obvious, and in the JavaScript example, it is not unless you already have a mental model from C...
@vaporeon_ @aescling C++ actually implicitly converts objects to references if the function is declared to take reference arguments
#include <iostream>
class T {};
void use_T(T& t) { std::cout << "I used a T!\n"; }
int main() {
T t;
use_T(t);
}
javascript wonkery
@wallhackio @vaporeon_ fur the record, the following alternative implementation is subtly but meaningfully diffurent:
const deleteThis = function() {
const temp = { _this: this };
delete temp._this;
};
arrow functions and functions defined with the function
keyword have intentionally diffurent rules about resolving this
@aescling @wallhackio @monorail Ms. Glaceon, please, we need that emoji
Maybe call it blobcat_eyeroll
, for consistency
@vaporeon_ @wallhackio @monorail “Ms. Glaceon”
@vaporeon_ @wallhackio @monorail makes her sound like a very tall elementary school teacher
@aescling @wallhackio @monorail Elementary school with a Python code-golfing class
@monorail @vaporeon_ @aescling hi ms. glaceon you are so cool
@monorail @vaporeon_ @aescling how is that confusing, it is simply an objective fact
@wallhackio @vaporeon_ @aescling i just don't understand the relevance to anything
@monorail @vaporeon_ @aescling it wasn't a reference to anything, you were just there and I felt like saying something nice
@wallhackio Nooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
And in standard JavaScript? No NPM?