Yes, in fact, all objects and arrays are pointers.
If I have to take your example literally, then, you only need to turn the variables to be of object types E.g.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pointers</title>
</head>
<body>
<h1>Asynchronous JavaScript</h1>
<!--the pointer with class b--->
<p>Our City</p>
<p id="variableA" >Minnesota</p>
<script src="pointers.js" ></script>
</body>
</html>
Then the pointers.js:
var a = {name: "todos", data: {pointerOne: 'Minnesota', pointerTwo: 'NewYork'}};
var b = a;//pointer to a
setTimeout(()=>{
//using timeout to see our pointer at work!
b.data.pointerOne = 'Nairobi';
document.getElementById('variableA').innerHTML = a.data.pointerOne;
}, 3000);
The major difference in JavaScript, is the way the pointers are actually presented or the lack of a direct way to identify pointers. In C, Golang, and C# pointers are made using the ‘&’ operator.
This is the basic. After getting this concept, you can now develop it further into function pointers, linked lists, or use it like structs; and do JavaScript the C++ way.
Though I would advice you to use the current JavaScript convention in naming your variables.
i.e
const a = {name: "todos", data: {pointerOne: 'Minnesota', pointerTwo: 'NewYork'}};
const b = a;//pointer to a
swap
function like in C :) – Kilometer[a, b] = [b, a];
– Antoineantoinettaa
andb
are references :) I mean you can't swap the values of primitives (for example numbers) in another function but you can do it easily in C with pointers – Kilometerswap(a, b)
, you[a, b] = [b, a]
. (And no, the behaviour doesn’t depend on whata
andb
contain.) – Antoineantoinetta[a, b] = [b, a]
everywhere. I mean that I can't write this in a some functionswap
. For example in C it costs nothing to add additional log about swapping the values of two variables but in JS I have to write this additional log everywhere when I swap the values of two variables – Kilometer