-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8-reference_data_types.js
More file actions
executable file
·178 lines (110 loc) · 4.96 KB
/
8-reference_data_types.js
File metadata and controls
executable file
·178 lines (110 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* REFERENCE DATA TYPES
Arrays, objects
In primitive data types, each time a variable is declared, a new memory is allocated and the value is stored in this new memory. Each time a variable is assigned a value, the value is copied into a new memory location. Therefore changing one does not affect the other since they are all in different memory adddresses.
*/
let x = 5;
let y = 10;
x = y; // x = 10;
y += 5; // y = 15, x = 10;
/*
In the reference data types, the data itself is not stored in the variable but rather a reference to the data is stored in the variable.
In this case, assigning one variable to another does not copy the entire object to the other variable but rather the pointer. As a result, both variables points to the same data and the value of one variable will affect the other as well.
*/
let ages =
{
kofi: 23,
Yaw: 15,
Ama: 11,
};
let footSize = ages; // pointer to object "ages" has been assigned to footSize. Two variables pointing to same object.
footSize.kofi += 2; // dereference(covertly) pointer to ages and modify object
ages.Ama = 12; // dereference(covertly) pointer to ages and modify object
console.log(ages); //output: [kofi: 25, Yaw: 15, Ama: 13]
console.log(footSize); //output: [kofi: 25, Yaw: 15, Ama: 13]
/* PRIMITIVE AND REFERENCE DATA TYPES AS FUNCTION ARGUMENTS
When you pass a primitive data type as an argument to a function, the argument is passed by value, that is the value of the variable is copied into the function parameters and therefore any change you make to the parameters does not affect the variable passed.
On the other hand, if you pass a reference data type as and argument to a function, it is passed by reference and therefore when you modify the parameter values, you indirectly modify the data passed as well.
*/
function updatePrice(item)
{
if (item != undefined)
{
if(typeof item == "object")
{
item.apple -= 1.89; // modifying argument passed
}
if (typeof item == "number")
{
item += 1.5; // modifying argument passed
}
return item;
}
}
spoon = 31.87;
fruits =
{
apple: 17.99,
orange: 4.59
};
console.log(updatePrice(spoon)); // 33.37
console.log(spoon); // 31.87 variable was not modified
console.log(updatePrice(fruits)) // [apple: 16.09, orange: 4.59]
console.log(fruits); // [apple: 16.09, orange: 4.59] object was modified by updatePrice()
/* The typeof operator is a unery operator used to determine the data type of its operand.
The operator returns a string of the the data type of its operand
syntax:
typeof operand
*/
function funcx()
{
return typeof "something";
}
console.log(typeof(typeof(true))); // string - the data type of the return value of typeof
console.log(typeof true); // boolean
console.log(typeof 4711); // number
console.log(typeof 'John Doe'); // string
console.log(typeof function () {}); // function
console.log(typeof {}); // object
console.log(typeof []); // object - array is of type object
console.log(typeof null); // object - null is of type object
console.log(typeof undefined); // undefined
console.log(typeof Symbol('B')); // symbol
/* NB: You cannot use the typeof operator to distinguish between reference types since they will all return objects. Eg it cannot be used to distinguish between an array and a normal object*/
/* USING INSTANCEOF OPERATOR TO DISTINGUISH BETWEEN REFERENCE TYPES
The instanceof operator evaluates if its first operand is an instance of its second operand and return either true or false.
syntax:
<operand> instanceof <datatype>
NB: it cannot be used to distaingush betweeen primitive types
*/
let numbers = [3,5,6,3,7,2,8];
let group =
{
name: "spartans",
location: "Tema"
};
function add(x, y)
{
return x + y;
}
let text = "9389ioj";
console.log("\nusage of instanceof:")
console.log(numbers instanceof Number); //false
console.log(numbers instanceof Array); //true
console.log(numbers instanceof Object); //true - arrays are objects
console.log(group instanceof Object); //true
console.log(add instanceof Function); //true
console.log(text instanceof String); //false
console.log(add instanceof Object) // true - functions are objects/ reference types
/* NB: Object is the type from which all other reference types are derieved. Therefore any reference type is an instance of an object*/
/* To check whether a variable is an array, the Array.isArray(variable) method can be used to check this. It's return value is a boolean*/
console.log(Array.isArray(text)) // false
console.log(Array.isArray(numbers)) // true
/* VARIOUS DEFAULT REFERENCE TYPES IN JS */
/*
1. Object - represent all types of objects
2. Array - represent arrays
3. RegEx = represent Regular Expressions that can be used to search for
partterns in a string
4. Date - represents data and time
5. Math - represent mathematical operations for calculations
*/