-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMember_Access_DOT_&_Square_Operator.js
More file actions
126 lines (95 loc) · 2.79 KB
/
Copy pathMember_Access_DOT_&_Square_Operator.js
File metadata and controls
126 lines (95 loc) · 2.79 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
// DOT Operator we can access only String Key
// [] Operator we can access String Key, Numeric Key, Special Key, Dynamic Key
var emp = {id:1001, name:'Ram'};
undefined
// Using DOT Operator OR (Member Access Operator)
emp.id;
1001
emp.name;
'Ram'
emp;
{id: 1001, name: 'Ram'}
typeof emp;
'object'
emp instanceof Object;
true
typeof Object;
'function'
// Using [ ] Operator (Associative Array Operator)
emp['id'];
1001
emp['name'];
'Ram'
emp;
{id: 1001, name: 'Ram'}
// Diff b/w [] & . Operator
// Using square Operator we can able to access or enter numeric key, as comapred to (.) Operator
var msdhoni = {name:'MS DHONI', age: 42, 100: 15, 50:52};
msdhoni.50; // we won't able to acess numeric key using DOT Operator
VM429:1 Uncaught SyntaxError: Unexpected number
msdhoni[50]; // we are able to acess numeric key using [] Operator
52
msdhoni[100];
15
msdhoni.city = 'Delhi';
'Delhi'
msdhoni;
{50: 52, 100: 15, name: 'MS DHONI', age: 42, city: 'Delhi'}
msdhoni.6 = 500; // we won't able to add numeric key using DOT Operator
VM571:1 Uncaught SyntaxError: Unexpected number
msdhoni[6] = 500; // we are able to add numeric key using [] Operator
500
msdhoni;
{6: 500, 50: 52, 100: 15, name: 'MS DHONI', age: 42, city: 'Delhi'}
// Special Character such as space we can add using [] Operator
msdhoni['current address'] = 'New Delhi'; // we are able to add key with space in b/w values using [] Operator
'New Delhi'
// Special Character such as space we can add using . Operator
msdhoni.full name = "M.S . Dhoni";
VM752:1 Uncaught SyntaxError: Unexpected identifier 'name'
msdhoni."full name" = "M.S . Dhoni"; // we are not able to add key with space in b/w values using DOT Operator
VM757:1 Uncaught SyntaxError: Unexpected string
// Access variable by using [] Operator
var key = "age";
undefined
msdhoni.key; // can't able to access using DOT Operator // this will directly lookup for key...
undefined
msdhoni[key]; // Able to access using [] Operator // this will first evaluate key...
42
// More Example
var dhoni = {id:7, name:'MS Dhoni'};
undefined
dhoni.id;
7
dhoni.name;
'MS Dhoni'
dhoni['id'];
7
dhoni['name'];
'MS Dhoni'
var dhoni = {id:7, name:'MS Dhoni', 100:10};
undefined
dhoni.100;
VM204:1 Uncaught SyntaxError: Unexpected number
dhoni[100];
10
dhoni.50 = 51;
VM265:1 Uncaught SyntaxError: Unexpected number
dhoni[50] = 51;
51
dhoni;
{50: 51, 100: 10, id: 7, name: 'MS Dhoni'}
dhoni.full address = "New Delhi";
VM375:1 Uncaught SyntaxError: Unexpected identifier 'address'
dhoni['full address'] = "New Delhi";
'New Delhi'
dhoni;
{50: 51, 100: 10, id: 7, name: 'MS Dhoni', full address: 'New Delhi'}
dhoni['full address'];
'New Delhi'
var r = 'id';
undefined
dhoni.r; // this will directly lookup for r...
undefined
dhoni[r]; // this will first evaluate r...
7