1

I'm wondering if there is a rationale behind this, since it seems to me like it would be more object-oriented if objects were passed as values. Effectively it would be a function scope for Objects, like with primitives.

Is just for optimisation purposes that objects are passed by reference?


ref: http://snook.ca/archives/javascript/javascript_pass

  • 2
    That doesn't differ from most (if not all) OOP languages. Why would passing by value be more OO ? – xlecoustillier Apr 13 '15 at 12:17
  • If you start passing by value, that implies cloning the object. This could be a very expensive operation if there's a large tree that needs to be cloned – James Thorpe Apr 13 '15 at 12:18
  • Maybe because you would have to do a deep clone of objects every time you pass it? – Ruan Mendes Apr 13 '15 at 12:19
  • @JamesThorpe To be fair - many times you can just move it rather than actually clone it - C++ does it (move semantics) and it often leads to very nice performance. – Benjamin Gruenbaum Apr 13 '15 at 12:20
  • @X.L.Ant Because an assignment of a global object to a local var would not mean that changes to the local var (which is a reference) would in effect be changes to the global object. They would then be changes to the local clone of the global object. So there would be a clear separation between local and global. – vkjb38sjhbv98h4jgvx98hah3fef Apr 13 '15 at 12:21
  • @DrMister this is true, but us understanding that we can do this efficiently is much newer than JavaScript. A future version of JavaScript will support value semantics for more than just primitive value types. – Benjamin Gruenbaum Apr 13 '15 at 12:23
  • @BenjaminGruenbaum Ok, I see. So basically the answer to my question is a combination of limitations of technical understanding at the time javascript was created, and optimisation purposes (i.e. not having to clone entire objects)? – vkjb38sjhbv98h4jgvx98hah3fef Apr 13 '15 at 12:27
  • @DrMister yes, but I want to make the fact that it's not a _real_ optimisation constraint in many problems. It's entirely possible to write fast code with value semantics in 99.9% of cases and help (in the form of value objects) is on its way as my answer explains. – Benjamin Gruenbaum Apr 13 '15 at 12:28
  • @BenjaminGruenbaum Exactly my point. It seems that these kinds of efficiency optimisation concerns should be secondary to architectural considerations. After all, a clear language architecture is also a very important optimisation, as it will lead to fewer mistakes being made. And processing power is forever increasing, but the human ability to handle complexity not as much. – vkjb38sjhbv98h4jgvx98hah3fef Apr 13 '15 at 12:33
  • Clear language architecture, JavaScript, choose one :D JavaScript is a really fun language but remember it was designed in 10 days by a single guy in urgency. The good stuff like value objects is on its way but we'll have to do with what we have right now. If it makes you feel any better a lot of other languages do this too (like Scala, Java and C#). – Benjamin Gruenbaum Apr 13 '15 at 12:36
  • @BenjaminGruenbaum :) Yeah, ah well, everything has its faults. Thanks for helping! – vkjb38sjhbv98h4jgvx98hah3fef Apr 13 '15 at 12:38

1 Answers1

3

What are we passing by?

JavaScript does not pass objects by reference. It passes objects by reference values like C# and Java. JavaScript is always pass by value - that value might just happen to be a reference to an object.

function foo(obj){
   obj = {};
}
var o = {x: 5};
foo(o);
console.log(o.x); // 5, proof that JavaScript is pass-by-value

For example, in C++:

void foo(int& i){
    i = 5; 
}
int j = 10;
foo(j);
std::cout << j; // logs 5, this is what pass-by-reference is

Primitives in JavaScript are values, passing them by reference would be meaningless since by being values they're immutable anyway (you can't change the number 5).

But values have much better!

I agree, values have much nicer semantics. You can't return value that will be modified from an unknown location by mistake. It's a lot clearer and it turns out it's very easy to optimize. Immutability in general is gaining a lot of momentum in JavaScript and a lot of people (me included) are using immutable-collections (like ImmutableJS). Note that non-primitive value types are planned but are currently under specification and won't arrive before ES7.

This is the only reference I could find about it, I haven't seen it discussed in the discussion mailing list but I hope it's on the way :)

Basically, the understanding that we can do this efficiently is much newer than JavaScript. JavaScript is a really fun language but remember it was designed in 10 days by a single guy in urgency. The good stuff like value objects is on its way but we'll have to do with what we have right now. If it makes you feel any better a lot of other languages do this too (like Scala, Java and C#).

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • This is technically accurate if you use the C++ definition of references. I don't think it actually addresses the OP's question – Ruan Mendes Apr 13 '15 at 12:20
  • There is no "C++ definition" of references, it's a single definition. reference means something very well defined: passing a reference to an object and not a value. JavaScript _does_ copy a value when you call a function - that value is a reference to an object. – Benjamin Gruenbaum Apr 13 '15 at 12:22
  • @BenjaminGruenbaum I did say that technically it is correct for languages that do support references. However, in Java, C# and JavaScript, that concept doesn't exist, and as you said, it passes value references. – Ruan Mendes Apr 13 '15 at 13:11