References In JavaScript

A reference is a pointer to an actual location of and object. The premise is that a physical object is never a reference.

In JavaScript we can allow the multiple variables can refer to the same object. Because it is the system of reference that JavaScript is based around. By maintaining sets of references to other object, the JavaScript affords us much more flexibility.

An object can contain set of properties, all of that are simply references to other objects. When multiple variables point to the same object, modifying the any variables or base variables takes reflects to other objects as well.

References in Class’s Objects Example:

class myClass{
	
	constructor(msg){
		this._msg = msg;
	}

	get msg(){
		return this._msg
	}

	set msg(m){
		this._msg = m;
	}
}

var obj1 = new myClass("obj1");
alert(obj1.msg); //obj1

var obj2 = obj1;
alert(obj2.msg); //obj1

obj1.msg = "New Value for obj1";
alert(obj1.msg); //New Value for obj1
alert(obj2.msg); //New Value for obj1

obj2.msg = "New Value for obj2";
alert(obj1.msg); //New Value for obj2
alert(obj2.msg); //New Value for obj2

As you seen in above example, every objects get reflects by modifying any one of the object. Here I used the JavaScript ES6 Class to create the class. Let’s see the one another example using Array class.

References in Array’s Objects Example:

var items1 = new Array("ONE", "TWO", "THREE");

var items2 = items1;

items1.push("NEW ONE");

alert(items1.length == items2.length); //true

items2.push("NEW ONE 1");

alert(items1.length == items2.length); //true

Here also, add new item in any of the object of array, it gets reflects to all the variables that point to the base object. Because the push method of the Array class stored its item to the properties of the Array class.

However if you change the reference of variables using the new keyword than now it will not get reflects is we change the old references properties.

var i1 = new Array( "A" , "B", "D");

var i2 = i1;

alert(i2 == i1); // true

var i1 = new Array( "E", "F" );

alert(i2 == i1); // false

References in String’s Objects Example:

If you give the reference to the string object, than remember one thing that if you change the base string object or any reference object value that it never get reflects any other object. Because we can not modifying or edit any string, we have to create a new string and assign to the variables.

Here I first create the one string with value “JavaScript” after that I give the reference to another string variables, now when I change the value of the base object by adding ” Hive” than the referenced object will never modified because by assigning new value to the base variables we just broke the reference.

And You can also say that it is impossible to give the reference to the string object. By giving the reference in string object we indirectly creating the new variable with the value of another variable.

var str1 = "JavaScript";

var str2 = str1;

alert(str1 == str2); //true

alert("Str1: " + str1 + " Str2: " + str2); //Str1: JavaScript Str2: JavaScript

str1 += " Hive";

alert(str1 == str2); //false

alert("Str1: " + str1 + " Str2: " + str2); //Str1: JavaScript Hive Str2: JavaScript

I hope you enjoy the tutorial, Please comment down your review about this.

Happy Coding!!!

🙂