References vs Values

Overview

Overview

All primitive data types in JS are copied by value
All objects (incl. arrays and functions) are copied by reference
references_vs_values picture

Copy by Value

Copy by Value

Example


		let x = 1;
		let y = x; // y stores the copy of "x" value

		// we change the value in "x", but that did not reflect "y"
		x = 9;
		console.log(`x = ${x}`);
		console.log(`y = ${y}`);
	

Copy by Reference

Copy by Reference

Demo with arrays


			let arr1 = [1,2,3];
			let arr2 = arr1; // "arr2" stores a reference to the "arr1"

			// we change a value in "arr1", and that WILL reflect the "arr2"
			arr1[0] = 9;
			console.log(`arr1: ${arr1}`);
			console.log(`arr2: ${arr2}`);

			// arr1: 9,2,3
			// arr2: 9,2,3
		

Demo with objects


			let user1 = {
				name: "Maria",
				age: 33
			}
			let user2 = user1; // "user2" stores a reference to the "user1" object

			// we change a value in "user1", and that WILL reflect the "user2" object:
			user1.name = "Pesho";
			console.log(`user1: ${user1.name}`);
			console.log(`user2: ${user2.name}`);

			// user1: Pesho
			// user2: Pesho
		

Copy Reference types

Copy Reference types

Shallow copy Array - spread operator - the easiest way


			let arr1 = [1,2,3];
			let arr2 = [...arr1];

			// we change a value in "arr1", and that WILL NOT reflect the "arr2" copy:
			arr1[0] = 9;
			console.log(`arr1: ${arr1}`);
			console.log(`arr2: ${arr2}`);
		

Shallow copy Array - arr.slice() method


			let arr1 = [1,2,3];
			let arr2 = arr1.slice();

			// we change a value in "arr1", and that WILL NOT reflect the "arr2" copy:
			arr1[0] = 9;
			console.log(`arr1: ${arr1}`);
			console.log(`arr2: ${arr2}`);
		

Shallow copy Object - spread operator - the easiest way


			let user1 = {
				name: "Maria",
				age: 33
			};
			let user2 = {...user1}; // make shallow copy

			// we change a value in "user1", and that WILL NOT reflect the "user2" object:
			user1.name = "Pesho";
			console.log(`user1: ${user1.name}`);
			console.log(`user2: ${user2.name}`);
		

Shallow copy Object - Object.assign() method


			let user1 = {
				name: "Maria",
				age: 33
			}
			let user2 = Object.assign({}, user1); // make shallow copy

			// we change a value in "user1", and that WILL NOT reflect the "user2" object:
			user1.name = "Pesho";
			console.log(`user1: ${user1.name}`);
			console.log(`user2: ${user2.name}`);
		

Reference: Object.assign() @MDN

Deep copy (clone) Reference types

Deep copy (clone) Reference types

JSON.parse/stringify

Shallow copies did not preserver the cahnge in inner levels, but deep copies does.

			let user1 = {
				name: "Maria",
				age: 33,
				colors:["red","green"]
			}
			let user2 = JSON.parse(JSON.stringify(user1));

			user1.colors[0] = "black";
			console.log(user2.colors);
		

Shallow Copy vs Deep Copy

Shallow Copy vs Deep Copy

Overview

Object Copying @wikipedia.org

Comparing reference types

Comparing reference types

If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
The equality and strict equality operators will both return true only if two variables point to the same object in memory.

Example


			let arr1 = [1,2,3];

			let arrRef = arr1;
			let arrCopy = [...arr1];

			console.log(`arr1 === arrRef: ${arr1 === arrRef}`);
			console.log(`arr1 === arrCopy: ${arr1 === arrCopy}`);

			// arr1 === arrRef: true
			// arr1 === arrCopy: false
		

These slides are based on

customised version of

Hakimel's reveal.js

framework