I have a doubt about the following codes. I thought that data-type primitives assigned to a variable in JS were passed by value, while objects were passed by reference. If functions are objects in JS, I thought they should be passed by reference, but the following two codes behave as passed by value:
CODE 1
var x = function hello(){ console.log("hello")};
console.log(x);
var y = x;
console.log(y);
x = function bye(){console.log("bye")};
console.log(x);
console.log(y);
OUTPUT
[Function: hello]
[Function: hello]
[Function: bye]
[Function: hello]
CODE 2
In this case I assigned a method to a global variable and the behavior is the same:
var x = {name:"Max",
action: function one(){console.log("one")}
}
var y = x.action;
console.log(y);
x.action = function two(){console.log("two")};
console.log(x.action);
console.log(y);
OUTPUT
[Function: one]
[Function: two]
[Function: one]
I truly appreciate any insight and explanation on this.