Javascript - Call by Sharing?

1 minute read

TIL that there is a programming language evaluation strategy called “call by sharing,” and that Javascript implements this approach.

As I understand it, call by sharing is a hybrid of the “call by value” and “call by reference” evaluation strategies that are implemented by other languages.

When a parameter is passed to a function in Javascript, that parameter cannot be reassigned to a new object or value within the scope of the called function: the original reference or value remains with the caller. A parameter, however, can be mutated within a called function’s scope (if the object is mutable) and the caller will see this mutation.

Example [1]:

var i = 25;
var obj1 = {val: "hello"};
var obj2 = {val: "hello"};

var update = function (j, o1, o2) {
	j = 200;
	o1.val = "goodbye";
	o2 = {val: "goodbye"};
}

update(i, obj1, obj2);

console.log(i); // remains as 25
console.log(obj1.val); // changed to "goodbye"
console.log(obj2.val); // remains as "hello"

If Javascript was purely call by value, then the mutation of obj1 would not be seen by the caller. If it was purely call by reference, then all three of the updates from the function above would have been seen by the caller. Call by sharing truly seems to be a hybrid of the two more commonly referenced evaluation approaches.

The name “call by sharing” is not colloquially used to describe this evaluation strategy. Java, Python and Ruby all implement call by sharing, but members of those communities do not reference it by its proper name. This inconsistency has led to confusion among users of multiple languages (myself included) and I’m glad I finally took some time to understand and sort out the differences.

Related Links:

Leave a Comment