-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.js
More file actions
80 lines (64 loc) · 1.45 KB
/
objects.js
File metadata and controls
80 lines (64 loc) · 1.45 KB
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
const point3D = new Object()
point3D.x = 5
point3D.y = 10
point3D.z = 20
const user = {
firstname: 'Alice',
lastname: 'Wonderland',
hobbies: [{id:1, title: "Hobbie1"}, {id: 2, title: "Tennis"}]
}
const point2D = {
x: 10,
y: 20,
showY: function() {
return `${this.y}`
}
}
console.log(point2D.x)
console.log(point2D['x'])
point2D.showX = function() {
return `${this.x}`
}
for (let key in point3D) {
console.log(`${key}: ${point3D[key]}`)
}
for (const[key, value] of Object. entries(point3D)) {
console.log(`${key}: ${value}`)
}
const cart = {
items: [],
addItem(item, price) {
this.items.push({item, price})
},
calculateTotal() {
return this.items.reduce((total, item) => total + item.price, 0)
}
}
cart.addItem('Milk', 2)
cart.addItem('Eggs', 5)
console.log(cart.calculateTotal()) // 7
// Object destructuring
const {x, y, z} = point3D
console.log(`${x}, ${y}, ${z}`)
// Without destructing - verbose
function display3DPoint(point) {
console.log(`x: ${point['x']}`)
console.log(`x: ${point['y']}`)
console.log(`x: ${point['z']}`)
}
// With destructing - not verbose
<<<<<<< HEAD
function display3DPoint2(x, y, z = 'Not provided') {
=======
function display3DPoint(x, y, z = 'Not provided') {
>>>>>>> origin/main
console.log(`x: ${x}`)
console.log(`x: ${y}`)
console.log(`x: ${z}`)
}
const p2 = {
x: 15,
y: 18,
z: 22
}
display3DPoint2(p2)