2

From study, I understood that in javascript, mutable objects() are treated by call-by-reference, and immutable objects are treated by call-by-value calling convention.

Let's say I use this kind of data,

var Node = function(data) {
  this.data = data;
  this.next = null;
};

var v = new Node(0);

is v a mutable object or an immutable object??

rokrokss
  • 137
  • 2
  • 11
  • What is immutable object? Do you mean primitive types? – Maheer Ali Apr 27 '19 at 13:15
  • 1
    Every parameter in JavaScript is passed by value. Your variable is an object. – Pointy Apr 27 '19 at 13:17
  • It is mutable as you can change/add/remove properties from the object – Patrick Evans Apr 27 '19 at 13:17
  • @Pointy Can you explain more _"Every parameter in JavaScript is passed by value"_. Consider a function `const f = (x) => x` And if we pass a object `{a:1}` to it. It will be passed by reference. – Maheer Ali Apr 27 '19 at 13:24
  • 1
    @MaheerAli Every object is a reference value, but still every argument is passed by value. If you call `f(y)`, then reassigning to `x` cannot change what `y` holds. – Bergi Apr 27 '19 at 13:25
  • @MaheerAli the terminology is confusing and unfortunate, and dates from before "objects" were things people talked about in programming. Almost all modern programming languages are pass-by-value because it's generally easier to implement and much less weird. – Pointy Apr 27 '19 at 13:27
  • @Bergi I think `call by sharing` is the right terminology for arguments? – Sagiv b.g Apr 27 '19 at 13:33
  • @Sagivb.g Yes, if we pass mutable things such as objects by value (without copying their contents), then we can also name that "call by sharing". – Bergi Apr 27 '19 at 13:38

4 Answers4

2

JavaScript don't have "immutable objects" there 2 types:

1) primitives - immutable

2) Object - mutable

update: object freeze can give some "immutable"

Tim Times
  • 422
  • 4
  • 8
1

It is mutable, as you can see from this working example.

var Node = function(data) {
  this.data = data;
  this.next = null;
};

var v = new Node(0);

v.myNewAttribute = 'foobar';

var elemDiv = document.createElement('div');
elemDiv.innerHTML = JSON.stringify(v);
document.body.appendChild(elemDiv);
Todd Chaffee
  • 6,754
  • 32
  • 41
1

First of all lets understand what is the new operator doing inside the created execution context behind the scenes:

It will:

  1. Create a new Object (and attach it to the this label)
  2. That new object's __proto__ property will reference the function's prototype property
  3. It will return the newly created object (if you are not explicitly returning an object)

So in your case:

var v = new Node(0);

v is actually an Object (the one that created and returned via new) and objects in JavaScript are mutable.

Here are the primitives (immutable) types:
Boolean
Null
Undefined
Number
String
Symbol (new in ECMAScript 6)

Sagiv b.g
  • 30,379
  • 9
  • 68
  • 99
1

v is a mutable object. To change this object to an immutable object use the Object.freeze() method.

Example:

var Node = function(data) {
  this.data = data;
  this.next = null;
};

var v = new Node(0); // "v" object is mutable  
v.data = 1;          // The "data" property value will change
console.log(v);

Object.freeze(v); // "v" object is immutable  

v.data = 2;       // The "data" property value will NOT change

console.log(v);

The Object.freeze() method freezes an object. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed. In addition, freezing an object also prevents its prototype from being changed. freeze() returns the same object that was passed in. more

Khalid Ali
  • 1,224
  • 1
  • 8
  • 12