-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototype.js
205 lines (169 loc) · 4.99 KB
/
prototype.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// new problem correct this
// function std(name,gender){
// this.name = name;
// this.gender = gender;
// }
// std.prototype.getBio = ()=>{
// console.log(this, this.name,'sfasd');
// }
// let std1 = new std("Himani","Female");
// std1.getBio()
// solution
// function std(name,gender){
// this.name = name;
// this.gender = gender;
// }
// std.prototype.getBio = function() {
// console.log(this, this.name,'sfasd');
// }
// let std1 = new std("Himani","Female");
// std1.getBio()
// other e.g
class People {
name;
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
setName(name) {
this.name = name;
}
}
let person = new People('Jon Snow');
console.log(person.getName()); // Jon Snow
person.setName('Dany');
console.log(person.getName()); //Dany
console.log(person); // People {name: "Dany"} __proto__: People
// example
let newVariable = function () {
this.a = 1;
this.b = 2;
};
let newObject = new newVariable(); // {a: 1, b: 2}
// add properties in newVariable function's prototype
newVariable.prototype.b = 3;
newVariable.prototype.c = 4;
// Now we can see that the properties that we assign to newVariable we can also use this with newObject variable.
console.log(newObject.a); // 1
console.log(newObject.b); // 2
console.log(newObject.c); // 4
console.log(newObject.d); // undefined
// example
// Adding Shared properties to Constructor’s .prototype Property and Define a constructor for Circle objects.
function Circle4(radius) {
this.radius = radius || 1.0;
this.getArea = function () {
return this.radius * this.radius * Math.PI;
};
}
var circle1 = new Circle4();
Circle4.prototype.getCircumference = function () {
return 2 * this.radius * Math.PI;
};
Circle4.prototype.color = 'green';
// The new properties are available to all instances, including existing ones
console.log(circle1.getCircumference());
console.log(circle1.color, circle1);
var circle2 = new Circle4(1.5);
console.log(circle2.getArea());
console.log(circle2.getCircumference());
console.log(circle2.color);
// example
var doSomething = function () {};
console.log(doSomething.prototype); // {} __proto__: Object
function doSomethingNew() {}
doSomethingNew.prototype.foo = 'bar'; // add a property onto the prototype
var doSomeInstancing = new doSomethingNew();
doSomeInstancing.prop = 'some value'; // add a property onto the object
console.log(doSomeInstancing); // prop: "some value"
// example
specified('hello world'); // => 'h e l l o w o r l d'
function specified(str) {
return str.split('').join(' ');
}
// place the specified function directly on the String object
String.prototype.specified = function () {
return this.split('').join(' ');
};
console.log('hello world'.specified());
// example
function Person() {}
Person.prototype.greeting = function () {
alert("Hi! I'm " + this.name.first + '.');
};
// console.log(Person.prototype, Person.greeting());
function Teacher() {}
Teacher.prototype = Object.create(Person.prototype);
Object.defineProperty(Teacher.prototype, 'constructor', {
value: Teacher,
enumerable: false, // so that it does not appear in 'for in' loop
writable: true,
});
console.log(Teacher.prototype);
Teacher.prototype.greeting = function () {
let prefix;
if (
this.gender === 'male' ||
this.gender === 'Male' ||
this.gender === 'm' ||
this.gender === 'M'
) {
prefix = 'Mr.';
} else if (
this.gender === 'female' ||
this.gender === 'Female' ||
this.gender === 'f' ||
this.gender === 'F'
) {
prefix = 'Ms.';
} else {
prefix = 'Mx.';
}
console.log(
'Hello. My name is ' +
prefix +
' ' +
this.name.last +
', and I teach ' +
this.subject +
'.'
);
};
let teacher1 = new Teacher(
'Dave',
'Griffiths',
31,
'male',
['football', 'cookery'],
'mathematics'
);
// console.log(teacher1.greeting(), 'teacher1')
// __proto__: doSomethingNew
// custom filter
Array.prototype.mfilter = function (fun) {
var filtered = [];
console.log(this); //output: [1,2,3,4,5,6]
console.log(fun);
// output:
// function (element, index, arr) {
// return element > 5;
// }
for (let i = 0; i < this.length; i++) {
if (fun(this[i], i, this)) filtered.push(this[i]);
}
return filtered;
};
var returnedArr = [1, 2, 3, 4, 5, 6].mfilter(function (element, index, arr) {
return element > 5;
});
console.log(returnedArr, 'returnedArr');
function object1() {}
function object2() {}
object1.prototype = Object.create(object2.prototype);
const object3 = new object1();
console.log(object1.prototype.isPrototypeOf(object3));
// expected output: true
console.log(object2.prototype.isPrototypeOf(object3));
// expected output: true