/
version03.js
109 lines (93 loc) · 2.33 KB
/
version03.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
// translated object
{
operatingSystem: 'Mac',
screenSize: '15 inches',
purchaseYear: 2011
}
// assigning an object to a variable
var myComputer = {
operatingSystem: 'Mac',
screenSize: '15 inches',
purchaseYear: 2011
};
console.log("Complete Object: ", myComputer);
console.log("Operating system: ", myComputer.operatingSystem);
console.log("Screen Size: ", myComputer.screenSize);
console.log("Purchase year: ", myComputer.purchaseYear);
// functions inside objects
var gordon = {
name: 'Gordon',
sayName: function() {
console.log(this);
}
};
console.log(gordon.sayName());
// refining this keyword based log
var gordon = {
name: 'Gordon',
sayName: function() {
console.log(this.name);
}
};
console.log(gordon.sayName());
// storing the todos
var todoList = {
todos: ['item 1', 'item 2', 'item 3']
};
console.log(todoList);
// creating a anonymous function to display todos
var todoList = {
todos: ['item 1', 'item 2', 'item 3'],
displayTodos: function() {
console.log('My Todos: ', this.todos);
}
};
console.log(todoList.displayTodos());
// creating a anonymous function to add todos
var todoList = {
todos: ['item 1', 'item 2', 'item 3'],
displayTodos: function() {
console.log('My Todos: ', this.todos);
},
addTodo: function(todo) {
this.todos.push(todo);
this.displayTodos();
}
};
console.log(todoList.addTodo('plunker'));
// creating an anonymous function to change todos based on position
var todoList = {
todos: ['item 1', 'item 2', 'item 3'],
displayTodos: function() {
console.log('My Todos: ', this.todos);
},
addTodo: function(todo) {
this.todos.push(todo);
this.displayTodos();
},
changeTodo: function(position, newValue) {
this.todos[position] = newValue;
this.displayTodos();
}
};
console.log(todoList.changeTodo(0, 'first'));
// creating an anonymous function to delete a todo
var todoList = {
todos: ['item 1', 'item 2', 'item 3'],
displayTodos: function() {
console.log('My Todos: ', this.todos);
},
addTodo: function(todo) {
this.todos.push(todo);
this.displayTodos();
},
changeTodo: function(position, newValue) {
this.todos[position] = newValue;
this.displayTodos();
},
deleteTodo: function(position) {
this.todos.splice(position, 1);
this.displayTodos();
}
};
console.log(todoList.deleteTodo(1));