1

Is there a way to pass an object by value in Javascript/NodeJS to a function? Or is the limitation built into the language?

1 Answers1

2

You cannot control how JS passes variables (afaik), so it will always pass by reference (some conditions may apply). The proper way to do this is to create a copy of the object and pass that in.

This functionality is built in to jQuery (not terribly hard to replicate).

var clone = function(object) { return $.extend(true, {}, object) };

This will create a deep copy of the object which is safe to pass into methods that may have unintended side-effects.

CollinD
  • 7,304
  • 2
  • 22
  • 45