0

I was in midst of reviewing some basic CS understandings but the question arose ;/ in my head.

I am wondering if there's an idea of passing by value or reference on 'functions'

Functions are one of the fundamental building blocks in JavaScript. A function is a JavaScript procedure—a set of statements that performs a task or calculates a value. -quoted from developer.mozilla

My current understanding is flustered in that when a function is saved: I do not understand how functions are passed.

Considering anything that is not primitive type is object type in javascript, the block of code is passed as reference to the function name. However, I am also told that

'Javascript always pass by value so changing the value of the variable never changes the underlying primitive (String or number).'

so does it pass by value? or by reference? so here was my attempt to test the case out:

function example(){
console.log('hello');
}

here block of code 'console.log('hello")' is saved to function variable named 'example'

then in the case of

var example2 = example;

I expected function 'example' to be passed as value to 'example2'

so I tried to test if my hypothesis is true by testing:

function original(){
    console.log('hello');
}
var Original = original;
original();
Original();

function original(){
    console.log('Hello');
}
original();
Original();

I honestly expected result to be

hello
hello
Hello
Hello

but instead the actual result was

Hello
Hello
Hello
Hello

I thought it was passed by reference but after reading the comment I realized that it was hoisting problem;

so I ran

var original = function(){
    console.log('hello');
}
var Original = original;
original();
Original();

original = function(){
    console.log('Hello');
}
original();
Original();

and the result came out to be

hello
hello
Hello
hello
soohyeok
  • 148
  • 7
  • 1
    Your second definition of `original` overwrites the first one. When javascript loads, functions gets moved to the top of the code (which is why you can assign a function even though it might not have been "declared" yet from the point of your code editor). I am not sure if there is a mechanism in which functions get moved to the top though, so it might be that once you get `hello`outputted 4 times and once `Hello` 4 times, but I don't know if there is an order of things – Icepickle Sep 10 '19 at 22:07
  • 1
    Pass by value/reference only applies when you call functions, it doesn't really apply to your code. You just assign a function to a different variable and attempt to reassign a new function to the old name. You might be interested in this question though: [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Ivar Sep 10 '19 at 22:22
  • ECMAScript always passes by value, which might be a primitive or a reference to an object. – RobG Sep 11 '19 at 01:00

1 Answers1

1

This is due to hoisting which is what happens when you use function declarations. If you use use function expressions, then what you expected will occur:

var original = function(){
  console.log('hello');
};
var Original = original;
original.value = 5;
original();
Original();

original = function(){ // changes original to point to a new function (does not affect Original)
  console.log('Hello');
}
original();
Original();
console.log( Original.value );
Matt Aft
  • 8,742
  • 3
  • 24
  • 37
  • Oh thank you :D I tried it just now and resulted in -------- hello hello Hello hello ----------- So I guess function is passed as value, not having affected the 4th hello to be 'Hello' – soohyeok Sep 10 '19 at 22:14
  • @soohyeok No, wrong [conclusion](https://jsfiddle.net/0wgn7hx5/) ;) You are reassigning the `original` variable, but `Original` already has a reference on the original `original` function – Icepickle Sep 10 '19 at 22:26
  • 1
    @Icepickle You're both right, actually. The _reference_ is passed _by value_ :) – Patrick Roberts Sep 10 '19 at 22:27
  • @icepickle oh so basically it is reference in that original was saved to location1 Original has pointer to location1 then original reassigned to location2 and Original still has pointer to location1? – soohyeok Sep 10 '19 at 22:27
  • 1
    @PatrickRoberts that's one way of saying it ^_^ – Icepickle Sep 10 '19 at 22:28
  • looks like the question was updated a bit, i only touched on the hoisting issue. I updated my answer based on your comments, please let me know if it's still not correct/clear – Matt Aft Sep 10 '19 at 22:33