Skip to content

Commit dece431

Browse files
committed
brushign up on objects and this/new keywords
1 parent 764f0d5 commit dece431

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

playground/new-this.html

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,83 @@
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width,initial-scale=1.0">
77
<title>New, This, Prototypes and Classes</title>
8-
<link rel="stylesheet" href="../base.css">
8+
<link rel="stylesheet" href="../base.css" type="html/css">
99
</head>
1010

1111
<body>
1212

13+
<button id="button1">Click Me!</button>
14+
<button id="button2">Click Me!</button>
15+
<script>
16+
// THIS KEYWORD
17+
18+
// add event listeners to button by using ID
19+
button1.addEventListener('click', tellMeAboutTheButton)
20+
button2.addEventListener('click', tellMeAboutTheButton)
21+
22+
// access button by using this keyword
23+
// note the differences in scoping between declaration and arrow functions
24+
// when a new function is made within another, it's scope is set to the window object
25+
function tellMeAboutTheButton(){
26+
console.log('outer: ',this)
27+
this.textContent = 'DoneClicked!'
28+
setTimeout(() => {
29+
console.log('innerArrow: ',this)
30+
}, 1000);
31+
setTimeout(function(){
32+
console.log('innerDeclaration: ',this)
33+
}, 1000);
34+
}
35+
36+
// OR
37+
38+
// access button by using event object
39+
function tellMeAboutTheButtonEvent(e){
40+
console.log(e)
41+
e.currentTarget.textContent = 'EVENTS RULE'
42+
}
43+
44+
</script>
45+
<script>
46+
// NEW KEYWORD
47+
48+
// create a new object model which takes in two arguments
49+
function Pizza(toppings = [], customer){
50+
// sets instances of new Pizza
51+
this.toppings = toppings;
52+
this.customer = customer;
53+
this.slices = 8;
54+
// creates methods attached to object model
55+
this.itsReady = function(){
56+
console.log(`Da pizza is a ready, come and get it ${customer}!`)
57+
}
58+
this.eatASlice = function(){
59+
if (this.slices > 0){
60+
this.slices--
61+
console.log('Mmmmm, delicious...😋')
62+
} else {
63+
console.log(`I'm out of 🍕`)
64+
}
65+
}
66+
}
67+
68+
const pepperoniPizza = new Pizza(['pepperoni'], 'David')
69+
pepperoniPizza.itsReady()
70+
console.log(pepperoniPizza)
71+
const ericsPizza = new Pizza(['cheese', 'onion', 'sausage'], 'Eric')
72+
console.log(ericsPizza)
73+
ericsPizza.eatASlice()
74+
ericsPizza.eatASlice()
75+
ericsPizza.eatASlice()
76+
ericsPizza.eatASlice()
77+
ericsPizza.eatASlice()
78+
ericsPizza.eatASlice()
79+
ericsPizza.eatASlice()
80+
ericsPizza.eatASlice()
81+
ericsPizza.eatASlice()
82+
83+
84+
</script>
1385
</body>
1486

1587
</html>

playground/objects.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,38 @@
1111
<body>
1212
<script>
1313

14+
// object literal syntax
15+
const person = {
16+
name: 'David',
17+
age: 33,
18+
clothing: {
19+
shirts: 10,
20+
shoes: 2
21+
},
22+
sayHello (greeting = 'Hola'){
23+
console.log(`${greeting}, I'm ${this.name}!`)
24+
}
25+
}
26+
// you can directly add new values using dot or bracket notation
27+
// bracked notation allows the key/value to be accessed through a variable dynamically
28+
console.log(person)
29+
person.isCool = false;
30+
person.isCool = true;
31+
console.log(person)
32+
person['married'] = false;
33+
console.log(person)
34+
delete person.married;
35+
console.log(person)
36+
person.sayHello()
37+
person.sayHello('What\'s good')
38+
39+
// loop over an object
40+
for (value in person){
41+
console.log(value + ': ', person[value])
42+
}
43+
44+
45+
1446
</script>
1547
</body>
1648

0 commit comments

Comments
 (0)