6

Is it generally acceptable (or frowned upon) in good JavaScript practice to modify a function parameter of type reference value (Object) from inside a function?

I’m aware of how it works in that all function parameters are passed by value and the reference value parameter is just a pointer to an Object’s memory location.

Thank you!

edit: I've removed "pass by reference" from title and description to make wording more correct and to avoid confusion for readers. Correct answer (and helpful comments) still apply

Dave
  • 2,126
  • 1
  • 15
  • 18
  • 4
    [That's not really what "pass by reference" means.](http://stackoverflow.com/questions/7744611) In any case, manipulating the contents of an object is a perfectly fine thing to do, assuming your system architecture is amenable to that. – Pointy Jun 29 '15 at 15:22

1 Answers1

5

You seem to know that it isn't passed by reference, so I can't imagine what "treating it" like it were would look like. Remember, because there is no pass-by-reference in JavaScript, that means you can't reach out from the function and change the variable whose value was passed in (not the object it refers to). It's neither good or bad practice; it's impossible.

function foo(a) {
   a  = {"different stuff": "here"};
}
var b = {stuff: "here"};
foo(b);               // `foo`'s assignment to `a` has nothing whatsoever
                      // to do with `b` and has no effect on it
console.log(b.stuff); // "here"

In general, it's perfectly acceptable to modify the state of an object through an object reference passed (by value) into a function, if that's what you're asking:

function foo(a) {
   a.stuff = a.stuff.toUpperCase();
}
var b = {stuff: "here"};
foo(b);
console.log(b.stuff); // "HERE"

That has nothing at all to do with pass-by-reference.

There are programming paradigms, some of which are sometimes used in JavaScript, where that would not be okay and you'd have to clone and return a new object. Those are not the norm in JavaScript, and if you're using them you'll know. If you're not, it's standard practice to modify object states like that.

Anh Cao
  • 482
  • 7
  • 13
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • "In general, it's perfectly acceptable to modify the state of an object through an object reference passed (by value) into a function, if that's what you're asking:" Yes this is better wording, forget pass by reference : ) So, in practice this is an acceptable technique to do? – Dave Jun 29 '15 at 15:59
  • 1
    @Dave: Unless you're using JavaScript for hardcore [functional programming](https://en.wikipedia.org/wiki/Functional_programming), yes. The vast majority of people don't. There is a larger "functional programming in JavaScript" community, though, than I've seen in other languages that weren't designed to be functional (as Haskell is, for instance). Not big in on a percentage basis, but still, they're there, But again, if you were using functional programming, you'd know. :-) – T.J. Crowder Jun 29 '15 at 16:05
  • 1
    Also thank you for the clarity that there is only pass by value in JavaScript. – Dave Jun 29 '15 at 16:06
  • Haha yes, definitely not functional, but I am writing JavaScript in a less common way. It's keyword driven automated testing framework that uses Sahi Pro to allow our QA team to write automated tests. In parallel to writing the framework, I'm reading 2 books, **Professional JavaScript for Web Developers** and **JavaScript: The Good Parts** which have been great reads. – Dave Jun 29 '15 at 16:18
  • 3
    @Dave: Just a word of caution on that second one. Crockford is a smart, well-informed person, but in his writing he fails sometimes to distinguish his opinion from fact. For instance, there is some flat-out incorrect information in the inheritance part of *The Good Parts* about constructor functions (which he dislikes). It's well worth reading Crockford, so long as you keep that in mind. :-) Separately, I'll just note that since writing that book, he's gone off in a much more functional direction, so if you read some of his more recent work, you'll see that influence. Best, – T.J. Crowder Jun 29 '15 at 16:33