2

I found that there are these two ways that a variable property value can be updated on a function call

Example 1:

function bar( arg ) {
  return arg + 1;
}

var foo = {
  num: 1
};

foo.num = bar( foo.num );
console.log( foo.num );

Example 2:

function bar( arg ) {
  arg.num = arg.num + 1;
}

var foo = {
  num: 1
};

bar( foo );
console.log( foo.num );

I want to know what are the proper naming convention for each of the method calls.

Also can anyone explain, how it is possible to update the original variable value inside a closed function operation as shown in example 2?

1
  • Well it is because objects are passed as reference so you're able to update from a closed function. Commented Dec 8, 2018 at 12:52

2 Answers 2

2

Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

If you pass an object (i.e. a non-primitive value, such as Array or a user-defined object) as a parameter and the function changes the object's properties, that change is visible outside the function. Source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Well in javascript objects are passed by reference so when you pass a object to a function you're passing it's memory reference not a copy. So when you update value in the function it updates the value at reference.

function bar(arg) {
  arg.num = arg.num + 1;
}

var foo = {
  num: 1
};

bar(foo);
console.log(foo.num);

When you pass a primitive value it is passed by value. It passes a copy of value so whatever changes you do in close function will not affect the original value.

function bar(arg) {
  arg = arg + 1;
}

var foo = 1

bar(foo);
console.log(foo);

Sign up to request clarification or add additional context in comments.

Comments

0

I want to know what are the proper naming convention for each of the methods.

There are no naming conventions for functions (I would only call them methods if they are directly associated to an object), except thar the name is camelCase. However there is a convention in functional programming that functions that return something are pure (they do not change anything, but just return something new, like your first function), and functions that return nothing are impure, they change something. Wether you strictly follow that pattern depends on your codingstyle, I follow that often but not always. It is also a bit harder in JS as JS is not typed.

also can anyone explain, how it is possible to update the original variable value inside a closed function operation as shown in example 2?

It is impossible. If you pass a number, it is passed as a value, you have no way to find out where that number belongs to. Thats a good thing as you can always keep track which function is able to mutate an object. bar(foo) is, bar(foo.num) is not.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.