Javascript
/
Objects
- 1 Fundamentals 11
-
Hello World S
-
Code structure S
-
Use strict S
-
Variables S
-
Data types S
-
Type conversions S
-
Maths S
-
Comparitions S
-
Conditional S
-
Loops S
-
Functions S
- 2 Testing 2
-
Mocha S
-
Nested describe S
- 3 Objects 4
-
Basics S
-
Reference S
-
Methods S
-
Constructor S
- 4 Types 5
-
Primitives S
-
Numbers S
-
Strings S
-
Arrays S
-
Json S
- 5 Classes 3
-
Constructor S
-
Binding S
-
Inheritance S
S
R
Q
Javascript Objects Reference
Variable, always copied as value Objects, stored by reference A = {x: 1}; B = A; B.x = 2; A.x == 2 // trueReference
Reference
A variable assigned to an object stores a reference to it.
/**
* Variable, alwayas copied as value
* Objects, stored by reference
*
* When we copy an object, the resulted two variables ...
* are references to tht same object
*/
let a = "Hello World";
let A = {
'name': 'John',
'age': 30,
}
let x = a; // copy by value
let B = A; // copy by reference
B.name = 'Sam'; // changes reference object
console.log(A === B); // true
console.log(A.name == 'Sam'); // true (copy by reference)
console.log(A.name != 'John'); // true
Equality
Two objects are equal only if they are the same object.
/**
* Two independent objects are not equal,
* even thought they look alike
*/
let A = {};
let B = {};
let C = B;
console.log(A == B); // false (different objects)
console.log(C === B); // true (reference to the same object)
➥ Questions