Hi @bytebodger
, I always like reading your posts, and the iconoclastic way you're doing it ;)
But here, I have to disagree (sorry !) when you say :
"You know what's another name for "pointer"??? Reference"
Pointers and references doesn't work the same way. When you reassign a pointer locally, the caller keep the former pointer. With a reference, if you reassign the variable, the caller gets the new value.
Take the following in C++ (a language which allow explicit pointers & allow you passing parameters by reference or by value) :
// Example program#include <iostream>
#include <string>
classSomeStuff{public:intsomeInt;SomeStuff(intanInt){someInt=anInt;}};voidreassignReference(SomeStuff&someStuff){someStuff=SomeStuff(42);// the referenced value has been changed}voidreassignPointer(SomeStuff*someStuff){someStuff=newSomeStuff(42);// the pointer is reassigned locally}intmain(){std::cout<<"This is call by reference !"<<std::endl;SomeStuffthree(3);std::cout<<"before reassignReference() : "<<three.someInt<<std::endl;reassignReference(three);std::cout<<"after reassignReference() : "<<three.someInt<<std::endl;std::cout<<"This is call by value !"<<std::endl;SomeStuff*four=newSomeStuff(4);std::cout<<"before reassignPointer() : "<<four->someInt<<std::endl;reassignPointer(four);std::cout<<"before reassignPointer() : "<<four->someInt<<std::endl;// the pointed object hasn't changed}
This is call by reference !
before reassignReference() : 3
after reassignReference() : 42
This is call by value !
before reassignPointer() : 4
before reassignPointer() : 4
Do we agree that if JS behave the same way than first portion, it means JS uses call-by-reference, if not it uses call-by-value ?
Let's try :
functionreassign(someStuff){someStuff={someInt:42};}console.log("This is call by -what- ?");letthree={someInt:3};console.log("before reassign",three);reassign(three);console.log("after reassign",three);
output :
This is call by -what- ?
before reassign {someInt: 3}
after reassign {someInt: 3}
It seems parameters are passed by value in JS.
Are we agree now ? :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi @bytebodger , I always like reading your posts, and the iconoclastic way you're doing it ;)
But here, I have to disagree (sorry !) when you say :
"You know what's another name for "pointer"??? Reference"
Pointers and references doesn't work the same way. When you reassign a pointer locally, the caller keep the former pointer. With a reference, if you reassign the variable, the caller gets the new value.
Take the following in C++ (a language which allow explicit pointers & allow you passing parameters by reference or by value) :
Do we agree that if JS behave the same way than first portion, it means JS uses call-by-reference, if not it uses call-by-value ?
Let's try :
output :
It seems parameters are passed by value in JS.
Are we agree now ? :)