0

I know typeof(my_number) is a primitive number, which makes functions receive a copy of my_number instead of my_number itself, which does't make it's value change at all. This is working as expected (I also know var is deprecated, that's probably not my issue here whatever).

What i don't know is why my_object_number isn't changed. I know i could have returned number + 1 and associated it to my_object_number, but I'm intentionally trying to code something similar to what a void(int*)function would have done in C.

var my_number = 3; 
var my_object_number = new Number(3);

var my_object ={
  number: 3
}
function increments_number(numero){
      numero = numero + 1; 
}

function increments_object(number){
  number.number++;
}
increments_number(my_number);
increments_number(my_object_number);
increments_object(my_object);

console.log("primitive number =  3, which increased by a function  = " + my_number);
console.log("object Number =  3 , which increased by a function = " + my_object_number)
console.log("prop number of a hardcoded object = 3, which increased by a function = " + my_object.number)

the code above prints.

primitive number =  3, which increased by a function  = 3
object Number =  3 , which increased by a function = 3
prop number of a hardcoded object = 3, which increased by a function = 4

I'm not sure why my_object_number isn't incremented

abcson
  • 157
  • 9
  • var is not deprecated... – Dan Oswalt Sep 27 '19 at 02:10
  • `my_object` You're not passing to any function, why it should change ? – Code Maniac Sep 27 '19 at 02:10
  • @NickParsons my_object is the one being passed to increments_object, my_object_number is passed to increments_number which does not use .number at all – abcson Sep 27 '19 at 02:10
  • @CodeManiac I've edited the code, sorry ahaha. F5 please, it works with my_object – abcson Sep 27 '19 at 02:11
  • 1
    Read [this answer](https://stackoverflow.com/a/5314911/707111) (not the top answer to the same question, which is misleading). – Ry- Sep 27 '19 at 02:22
  • 1) `increments_number` still only assigns to its local `numero` variable, it doesn't even try to mutate an object 2) `new Number` objects are immutable – Bergi Sep 27 '19 at 02:43
  • @Bergi: Nitpick: they’re mutable in that you can change their properties, just not their inherent numeric value – and by overriding their `valueOf`, you can make things pretty weird too :) – Ry- Sep 27 '19 at 03:08
  • @Ry- Yeah, it's better to be accurate - thanks for the correction – Bergi Sep 27 '19 at 03:26

1 Answers1

0

I know typeof(my_number) is a primitive number, which makes functions receive a copy of my_number instead of my_number itself, which does't make it's value change at all.

There is no difference between primitives and objects in JavaScript in this respect. Actually, you can forget there’s a difference at all, for the most part – in strict mode, they’ll either behave the same in every situation or throw an error. Function calls aren’t related either. Instead, the difference is between mutating a value and reassigning something that contains a value, like a variable or a property, to contain a different value.

var a = {};
var b = a;
b = {foo: 'bar'};

console.log(a);  // didn’t change, because assignment to `b` changes the variable `b`,
                 // not the value `{}`


var d = 5;
var e = d;
e = 6;

console.log(d);  // didn’t change, because assignment to `e` changes the variable `e`,
                 // not the value `5`

'use strict';

var a = {};
var b = a;
b.foo = 'bar';

console.log(a);  // the value {} changed; all variables stayed the same


var d = 5;
var e = d;
e.foo = 'bar';   // throws an error; a primitive is indistinguishable from…


var f = Object.freeze({});
var g = f;
g.foo = 'bar';   // … an immutable object.

++, like all math operators in JavaScript, is an operation on something that contains a value – it doesn’t mutate a value. ++x acts like x = x + 1.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • `"There is no difference between primitives and objects"`. One big difference is that primitives are passed by value and objects are passed by reference. – Cully Sep 27 '19 at 02:30
  • 1
    @Cully: That’s incorrect. JavaScript is exclusively a pass-by-value language. You can say that objects *are* references or say that objects *are* pointers to liken them to languages like C#, Python, C… but in JavaScript, the differences between primitives and objects are non-behaviour things like boxing in sloppy mode, ability to use as keys of `WeakMap`s/`WeakSet`s, ability to pass to `Object.keys`, etc. – Ry- Sep 27 '19 at 02:32
  • @Cully: Put another way, if objects were pass by reference, `function foo(x) { x = {foo: 'bar'}; } var y = {}; foo(y)` would change which value `y` contained. That’s what it means to pass by reference. JavaScript doesn’t have that concept. (The “pass” in “pass by reference” refers to passing arguments to function calls, but arguments and variables always behave the same.) – Ry- Sep 27 '19 at 02:34
  • `function modify(c) {c.c && c.c++ || c++}; var d = 3, e = {c:3};modify(d); modify(e); console.log(d,e)` changes done to primitive and changes done to object what about this then? @Ry- – joyBlanks Sep 27 '19 at 02:52
  • @joyBlanks: An object without a `c` property would behave the same way as the number there. You’re choosing a different action each time, so of course the results will be different. – Ry- Sep 27 '19 at 02:54
  • I just wanted to say when you make changes to a primitive variable it is not impacted in parent scope but if changes to an object it makes impact to parent scope. No?. I just wrote 1 method for past by value and reference – joyBlanks Sep 27 '19 at 02:55
  • @joyBlanks: Based on the truthiness of `c.c`, you either perform `c.c++` (an operation that modifies the value in `c`) or `c++` (an operation that does not modify the value in `c`). Primitives/objects are unrelated to how it behaves. If instead you *always* did `c++`, nothing would happen in either case, and if you *always* did `c.c++`, the primitive version would be indistinguishable from an immutable (non-primitive) object. – Ry- Sep 27 '19 at 02:58
  • aint that the classic definition of pass by value and pass by reference ? – joyBlanks Sep 27 '19 at 03:00
  • we polluted the answer with too much chat. but I see what you mean, but still bouncy. anyways thanks :) – joyBlanks Sep 27 '19 at 03:02