-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_objects.js
163 lines (114 loc) · 4.81 KB
/
10_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
/*-----------------------------------------------------------------------------*/
/* Objects and prototypes
-------------------------------------------------------------------------------*/
// https://medium.com/backticks-tildes/javascript-prototypes-ee46810e4866
// https://chamikakasun.medium.com/javascript-prototype-and-prototype-chain-explained-fdc2ec17dd04 !!!
let age = 20;
let testObj = {
firstname: "Dimon",
lastname: "Anikin"
}
testObj.age = age;
console.log(testObj);
// __proto__
// equals
// Object.getPrototypeOf()
// Person === Person.prototype.constructor
// Person.prototype === Person.prototype.constructor.prototype
// Constructor function
// Creates empty object and attaches passed arguments as object's properties
function Cat(name, color){
this.name = name
this.color = color
}
// Prototype is like a "higher-order" object where we can put methods to make them accessible to all the children.
// Because it doesn't make sense to place the exact same function in every instance of an object
Cat.prototype.voice = function(){
console.log(`Cat ${this.name} says meow`);
}
const cat = new Cat("Dimon", "white")
console.log(cat); // object, has only initial properties
// Getting prototypes object
console.log(typeof cat.__proto__); // object
console.log(Cat.prototype); // same as above
console.log(cat.constructor);
cat.voice()
// When an object gets a request for a property that it does not have, its prototype
// will be searched for the property, then the prototype’s prototype, and so on.So who
// is the prototype of an object ? It is the great ancestral prototype, the entity behind
// almost all objects, Object.prototype.Many objects don’t directly have Object.prototype
// as their prototype, but instead have another object that provides a different set of
// default properties.Functions derive from Function.prototype, and arrays derive from
// Array.prototype and so on.
/*-----------------------------------------------------------------------------*/
/* Why prototypes are important
-------------------------------------------------------------------------------*/
let protoRabbit = {
color: 'grey',
speak(line) {
console.log(`The ${this.type} rabbit says ${line}`);
}
};
let killerRabbit = Object.create(protoRabbit);
killerRabbit.type = "assassin";
killerRabbit.speak("SKREEEE!");
// The method speak in the code above is inefficient because if you were to create multiple
// rabbit objects, you would have the same function written everywhere and that’s where
// prototype and constructor functions really come in.
function ProtoRabbit(color, words, type) {
this.color = color;
this.words = words;
this.type = type;
};
ProtoRabbit.prototype.getColor = function () {
return this.color;
}
ProtoRabbit.prototype.speak = function () {
console.log(`The ${this.type} rabbit says ${this.words}`);
}
// let killerRabbit = new ProtoRabbit('grey', 'SKREEEEE!', 'assassin');
// killerRabbit.speak();
/*-----------------------------------------------------------------------------*/
/* Constructor functions inheritance
-------------------------------------------------------------------------------*/
function Person(firstname, birthYear){
this.firstname = firstname
this.birthYear = birthYear
}
Person.prototype.getAge = function(){
return 2022 - this.birthYear;
}
// Linking Constructor Functions
function Student(firstname, birthYear, course){
Person.call(this, firstname, birthYear)
this.course = course
}
// Linking prototypes
Student.prototype = Object.create(Person.prototype)
/*-----------------------------------------------------------------------------*/
/* There are constructor function methods and prototype methods
-------------------------------------------------------------------------------*/
// function Person(){}
// Person.prototype.legs = 2;
// Person.prototype.skin = "white"
// const person = new Person()
// person.name = "Dimon"
// console.log("skin" in person); // true
// console.log(person.name); // Dimon
// console.log(person.legs); // 2
/*-----------------------------------------------------------------------------*/
/* How to know where are constructor function methods and where are prototype ones?
-------------------------------------------------------------------------------*/
// console.log(person.hasOwnProperty("name"));
// console.log(person.hasOwnProperty("skin"));
/*-----------------------------------------------------------------------------*/
/* Keyword new
-------------------------------------------------------------------------------*/
// this is how new keyword works natively
function myNew(constructor, ...args) {
const obj = {}
Object.setPrototypeOf(obj, constructor.prototype)
return constructor.apply(obj, args) || obj
}
const cat2 = myNew(Cat, "black", "Fur")
console.log(cat2);