Stocking Out - Print module


#21

That’s because you send the array directly to the function, they don’t need to get it from your global variable

when you do a = 100 you’re not changing the value itself, you’re replacing it

Also, you’re printing the return value of a function which doesn’t return anything so no, nothing would ever be printed for the first call to console.log


#22
function foo(a) {
    a.push(100);
}
a = [4, 5]
console.log(foo(a));
console.log(a);
undefined
[3, 5, 100]

#23

And if you’re saying that a is global in the function, that’s easily disproved by changing the name and still getting the same outcome. It’s not accessing the global variable.


#24

But how is the global getting changed if it is not being accessed?


#25

Because you’re handing the array to the function.

You’re providing two ways to access that array and only cutting off one of them

There’s no reason for you to send the array as an argument if what you mean to show is that it can access it anyway.


#26

Except the function has not return value. It must be acting directly upon the global.


#27

Variables are not their values


#28

Which brings us back to reference objects not being copied to function scope the way primitive values are.


#29

No such thing as reference object
And primitives are very unlikely to be copied, at least strings which could be large


#30

What do we call arrays and objects, then? Don’t they provide a reference to the values and items they contain? Where did I ever get that term? Sure as heck didn’t make it up.

D’oh!

https://www.ecosia.org/search?q=reference+objects


#31

It’s all by reference, and if it’s not, you can’t tell, so there’s no difference between primitives and other values in that regard

Objects are not global or local or anything else such
Names are

Objects don’t float around globally, you’d have no way to grab them

The runtime puts them somewhere you can’t address, and gives you a reference

When all references to an object are gone, the value eventually gets garbage collected


#32

When you get to my age, it becomes difficult to remember what was learned long ago and what is learned (or re-learned in the present). Seems to be more and more of a problem for me as the years pass. I self-taught myself a lot of misinformation in the early years and knowing better sometimes isn’t enough to push me off the old track.

Are these references a kind of stub file entry? Or just loosely contained and dependent upon some reference in the code?


#33

Same as python, every scope is essentially a dict, and the real values aren’t in the dict

So the real values aren’t in the scope at all
And there’s no copying of values, unless you explicitly create a new value and copy
All that’s ever copied otherwise is the references
If something is to be held private, make a copy and send that.


#34

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.