javascript - Is it good practice to manipulate a function parameter from within a function? -


is acceptable (or frowned upon) in javascript practice modify function parameter of type reference value (object) inside function?

i’m aware of how works in function parameters passed value , reference value parameter pointer object’s memory location.

thank you!

edit: i've removed "pass reference" title , description make wording more correct , avoid confusion readers. correct answer (and helpful comments) still apply

you seem know isn't passed reference, can't imagine "treating it" like. remember, because there no pass-by-reference in javascript, means can't reach out function , change variable value passed in (not object refers to). it's neither or bad practice; it's impossible.

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

in general, it's acceptable modify state of object through object reference passed (by value) function, if that's 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 @ pass-by-reference.

there programming paradigms, of used in javascript, not okay , you'd have clone , return new object. not norm in javascript, , if you're using them you'll know. if you're not, it's standard practice modify object states that.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -