-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathprotypes.js
95 lines (75 loc) · 2.16 KB
/
protypes.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
// prototypes manipulation
String.prototype.alt = 'another way'
String.prototype.altSchool = function() {
return 'altSchool'
}
let name = 'oluwasetemi' // literal 1 - use constructor to construct a new variable, 2 - converts the constructed object to a primitive data type
// attach properties and methods to the new primitive data type
console.log(typeof name);
let ab = 'good'
console.log(ab.alt)
console.log(ab.altSchool())
console.log(name.capitalize())
String.prototype.capitalize = function() {
const first = this.charAt(0).toUpperCase()
const rest = this.slice(1)
return first + rest
}
console.log(name.altSchool())
let a = 2;
// a.altSchool()
// contructors
// let button = new HTMLButtonElement()
// console.log(button)
// String, Number, Boolean, Array, Object, Function, Promise, RegExp, Date, Error etc
let now = new Date()
let before = new Date('2020-01-01')
let before2 = new Date(2020, 0, 1, 23, 59, 59, 999)
console.log(before2)
let err = new Error('this is an error')
console.log(err.stack)
console.log(now.getDate())
console.log(Math.PI)
// oven that makes bread
function Ojo(name) {
// if (!new.target) {
// return new Ojo(name)
// }
// let this = {}
// property
this.name = name
// method
this.giveBirth = function(name) {
console.log('give birth')
console.log(name)
}
// return this
}
let bread1 = new Ojo('oluwasetemi')
console.log(bread1)
console.log(bread1 instanceof Ojo)
let whiteBread = new Ojo('white')
let agegeBread = new Ojo('agege')
// bread
const ojo = {
name: 'oluwasetemi',
giveBirth: function(name) {
console.log('give birth')
console.log(name)
}
}
// copying shallow copy, deep copy JSON.parse(JSON.stringify(obj)), Object.assign({}, obj), spread operator, lodash, StructuredClone()
const setemi = new Ojo('oluwasetemi')
function add(a, b) {
return a + b
}
// Closure - access to a closed varible during currying
function addTrouble(a) {
// a is only supposed to be accessible within the scope of addTrouble
// but because of closure, it is accessible to the returned function - closed variable
return function(b) {
return a + b
}
}
console.log(add(1, 2))
console.log(addTrouble(10)(20))