forked from sumitkumar2019/CompleteJavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.js
More file actions
155 lines (119 loc) · 3.95 KB
/
objects.js
File metadata and controls
155 lines (119 loc) · 3.95 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
/**Creating an Object using Object literals */
const a={}; // {} - This is a object literal
const circle = { // An obect has members like properties and methods
//properties of an object
radius:1,
location:{
x:1,
y:2
},
//Technically this should be called as method instead of function
//because it is inside an object
draw: function(){
console.log('draw');
}
};
circle.draw();
console.log(circle.constructor);
/**Creating an Object using Factories */
function createCirle(radius, location){
return {
radius:radius,
location:location,
draw(){
console.log('draw from factory created object');
}
};
}
const circle1 = createCirle(5, {x:1, y:2});
circle1.draw();
console.log(circle1.constructor);
/**Creating an Object using Constructors function*/
function Circle(radius){
//console.log('this', this);
this.radius = radius;
this.draw = function (){console.log('draw from constructor')}
}
const circle2 = new Circle(2); //create an object using constructor
const circle3 = Circle(2); //treat regular function instead constructor global object will get modify instead of creating an object
circle2.draw();
console.log(circle2.constructor);
/**Built in Constructors */
new Object(); //when we create an object using object literals let x ={}, internally it call new Object()
new String();
new Boolean();
new Number();
/**Functions are Objects: calling its inbuilt properties and methods */
console.log(Circle.name);
console.log(Circle.constructor);
console.log(Circle.length);
console.log(Circle.prototype);
/** If the function f was invoked by the top level code, the value of f.caller is null ,
* otherwise it's the function that called f .
* This property replaces the obsolete arguments.caller property of the arguments object */
console.log(Circle.caller);
console.log(Circle.toString());
Circle.call({x:1}, 1);
Circle.apply({x:1}, [1]);
Circle.bind({x:1})();
const Circle1 = new Function('radius', `
this.radius = radius;
this.draw = function (){console.log('draw from Funtion constructor')};
`);
const circle4 = new Circle1(1);
console.log(circle4.constructor);
console.log(circle4);
circle4.draw();
/**Value Types vs Reference Types */
//Primitive type: Copied by value: Call by value
let x =10;
let y =x;
x=20;
console.log(x);
console.log(y);
//Reference type : Copied by reference: Call by reference
let k = {x:20};
let l = k;
k.x=30;
console.log(k);
console.log(l);
/**Adding properties of an object */
circle.size=10;
console.log(circle);
// we can easily use bracket notation for properties which have (space or - or special character)
const propertyName = 'diameter length';
circle[propertyName] = 20;
console.log(circle);
/**Removing properties of an object */
delete circle.size;
console.log(circle);
/**Enumerating properties */
for(key in circle) {
if(typeof circle[key] !== 'function'){
console.log('key: '+ key +' value: ' + circle[key]);
}
}
const keys = Object.keys(circle);
console.log(keys);
const values = Object.values(circle);
console.log(values);
const entries = Object.entries(circle);
console.log(entries);
if('radius' in circle) console.log('circle has a radius');
/**Abstraction: Hide the details-Show the essentials
* Private properties and methods using let instead of this
* Closure properties
* */
function Circle9(radius){
this.radius = radius;
let defaultLocation = {x:10,y:10}; // Private attribute
let computeLocation = function(){console.log('private compute location method')};// Private method
this.draw = function(){ //child funtion
let x,y; /// local scope variable and will garbage collected
console.log(defaultLocation);///closure properties from parent object and they will stay in memory
computeLocation(); ///closure properties from parent object and they will stay in memory
console.log('draw');
}
}
const circle9 = new Circle9(5);
circle9.draw();