-
Notifications
You must be signed in to change notification settings - Fork 0
/
Objects.js
174 lines (143 loc) · 5.08 KB
/
Objects.js
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
169
170
171
172
173
174
// The Secret Life of Objects
/*
"An abstract data type is realized by writing a special kind of program [...]
which defines the types in terms of the operations which can be performed on it"
*/
// Encapsulation
/*
The core idea in object-oriented programming is to devide programs into smaller pieces
and make each piece responsibe for managing its own state.
inspired by how cells were encapsulated by a membrane and communicated via message passing.
All those ideas came together to form the the foundations of OOP:
encapsulation and message passing.
*/
//Method
/*
Methods are nothing more than properties that hold function values. This is a simple method.
*/
let rabbit = {};
rabbit.speak = function (line) {
console.log(`The rabit says '${line}'`);
};
rabbit.speak("I'm alive.");
/*
Usually a method needs to do something with the object it was called on.
When a function is called as a method—looked up as a property and immediately called,
as in object.method()—the binding called this in its body automatically points at the object
that it was called on.
*/
function speak(line) {
console.log(`The ${this.type} rabit says ${line}`);
}
let whiteRabbit = {type: "white", speak}
let hungryRabbit = {type: "hungry", speak}
whiteRabbit.speak("Oh my ears and whiskers, "+"How late it's gettting!");
hungryRabbit.speak("I could use a carrot right now.");
/*
You can think of this as an extra parameter that is passed in a different way.
If you want to pass it explicitly, you can use a function’s call method,
which takes the this value as its first argument and treats further arguments
as normal parameters.
*/
speak.call(hungryRabbit, "Burp! ");
/*
Since each function has its own this binding, whose value depends on the way it is called,
you cannot refer to the this of the wrapping scope in a regular function
defined with the function keyword.
*/
/*
Arrow functions are different—they do not bind their own this but can see the this binding
of the scope around them. Thus, you can do something like the following code,
which references this from inside a local function:
*/
function normalize() {
console.log(this.coords.map(n => n/this.length));
}
normalize.call({coords: [0, 2, 3], length: 5});
// If I had written the argument to map using the function keyword, the code wouldn’t work.
// ProtoTypes
let empty = {};
console.log(empty.toString);
console.log(empty.toString());
/*
Pulled a property out of an empty object. magic!
not exactly, I have simply been withholding information about the way js object work.
In addition to their set of properties, most object also have a prototype.
A prototype is another object that is used as fallbak source of properties.
When an object get a request for a property, then the prototype's prototype, and so on.
So, who is the prototype of the empty object?
Its is the great ancestral prototype, the entity behind almost all object.prototype
*/
console.log(Object.getPrototypeOf( {} ) === Object.prototype);
console.log(Object.getPrototypeOf(Object.prototype));
// You can use Object.create to create an object with a specific prototype.
let prototyRabbit = {
speak(line){
console.log(`The ${this.type} rabbit says '${line}`);
}
};
console.log("test", this.prototyRabbit);
let killerRabbit = Object.create(prototyRabbit);
killerRabbit.type = 'killer';
killerRabbit.speak('Awesome!');
/**
* A prototype like speak(line) in an object expression is a shorthand way of defining a method.
* It create a property called speak and gives it a function as its value.
*
* The prototy rabbit acts as a container for the properties that are shared by all rabbits.
* an individual rabbit object, like the killer rabbit, contains properties that apply only
* to itself in the case its type and derives shared properties form its prototype
*/
// -> Classes
function pick(arr) {
const idx = Math.floor(Math.random()*arr.length);
return arr[idx];
}
function getCard() {
const values = [ '1', '2','3','4','5','6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
const suits = ['clubs', 'spades', 'hearts', 'diamonds'];
const value = pick(values);
const suit = pick(suits);
return{
value,
suit
};
}
console.log('Random Call getCard: ',getCard());
console.log('Random Call getCard: ',getCard());
console.log('************************');
// Method
// we can add function as properties on objects
const math = {
multiply: function (x, y) {
return x*y;
},
divide : function (x, y) {
return x/y;
},
square: function (x) {
return x*x;
}
}
console.log(math.multiply(2, 2));
const newMath = {
// shorthand
add(x, y){
return x+y;
},
sub(x, y){
return x-y;
}
}
console.log(newMath.add(2, 2));
const auth = {
username: 'ullas',
logIn(){
console.log('Loged in');
},
logOut(){
console.log('Loged in');
}
}
console.log(auth.logIn);
auth.logIn();