Comments (8)
The correct term is "call by sharing" or "pass by sharing."
As has been said, while pass-by-sharing uses pass-by-value technically, the value is a reference. It is sort of a mix of pass-by-value and pass-by-reference. It isn't as helpful to call it pass-by-value as it is to call it pass-by-reference because it behaves like the latter, despite technically being a form the former. But because it isn't just pass-by-value, it is unhelpful to call it that. "Pass-by-sharing" is the correct term and tells the full story.
from 33-js-concepts.
indeed pass by sharing is the correct term as mentioned in my original comment.
from 33-js-concepts.
Objects and Arrays are passed by reference. As in, passing in an object or an array in a function will alter the reference object as well. But if you pass in an integer and do something with it inside the function, it won't get altered outside.
let i = 10;
console.log("Init value", i);
// Changing the value of i inside the function
function changeVal(i){
i = 20;
console.log("Inside function", i);
}
// Invoke function, passing in i as value
changeVal(i);
// Printing the value of i after the function.
// i is still equal to 10.
console.log("After function call", i);
// =========================================
// Create a new obj, prop name = "John".
let obj = {name : "John"};
console.log("Init value", obj.name);
// Changing the name of the property inside the function
function changeName(obj){
obj.name = "Brad";
console.log("Inside function", obj.name);
}
// Invoking the function with the object.
changeName(obj);
// Printing name prop after the function.
// The name is now "Brad".
console.log("After function call", obj.name);```
from 33-js-concepts.
I have to disagree. In a pass by reference language the folling code would alter the original object:
let foo= new Object();
foo.name = "foo";
function changeObj(obj){
obj = new Object();
obj.name = "bar";
}
changeObj(foo);
console.log(foo.name);
//foo
However in JS this is not the case because obj is merely a variable that holds a value (the reference) and is not the actual reference itself. Its just confusing that javascript has called them references but that doesnt change the underlying principle here. When you do a pass by reference in c++ foo would hold the new Object here.
from 33-js-concepts.
That is correct.
It's passed as a copy of the reference, so you could still alter the original object within a function and it would be altered outside of it.
But if you re-assign it you are just changing the copy of the reference, so then it won't be available outside of the function.
By changing it inside the function you are just telling the obj to point at a new reference and then set that name to "bar", however that scope dies and so does that new reference point.
In short, I agree. It's not really passed by reference, it's passed as a copy of the reference.
from 33-js-concepts.
saying
It's passed as a copy of the reference
is very misleading. Again: no copy is made at any point. the only thing that happens is when you write the dot operator (.) is that javascript's interpreter will perform a get - more information here and here . Then you can change whatever that object contains using set.
from 33-js-concepts.
it is passed by value every time. No exception and no copies. The only thing that is confusing is that the value that is passed is a reference itself. However pass by reference means something very different.
from 33-js-concepts.
The correct term is "call by sharing" or "pass by sharing."
Perfect! That was the term I was looking for, thanks for sorting it out :)
from 33-js-concepts.
Related Issues (20)
- for contributing in beginner project list with ToDo App HOT 2
- There is no Marathi Translation of document HOT 3
- There is no Gujarati Translation of document HOT 2
- Hindi language translation mentioned twice in README.md HOT 1
- Add Topic : JS Web APIs HOT 7
- There is no sindhi language translation of this documentation.
- There is no Sanskrit translation HOT 1
- No Sanskrit(संस्कृत) Translation HOT 2
- Add videos to understand concept more easily HOT 9
- [issue] : Is there any alternative of useEffect in js.
- Adding video tutorial for Clean Code section in README.md HOT 1
- Would like to suggest the addition of few links
- add Dependabot HOT 3
- Add interview preparation section in each tag HOT 5
- React async nature
- Missing Hebrew translation HOT 1
- Question 3 "Value Types and Reference Types" is wrong, meaningless, and misleading HOT 2
- Missing video icons HOT 2
- Whole java
- JS
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 33-js-concepts.