-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearning OOP.js
More file actions
153 lines (124 loc) · 3.56 KB
/
Copy pathlearning OOP.js
File metadata and controls
153 lines (124 loc) · 3.56 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
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
// 1. Accessing the a nested property using a for loop
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};
function countOnline(obj) {
var count = 0;
for (let user in obj) {
if (obj[user].online === true) {
count++; //note here the a variable passed into
//a bracket notation is only followed by a dot notation
}
}
return count;
}
console.log(countOnline(users));
//2. set function argument friend to friends property and return array of friend
let user = {
name: 'Kenneth',
age: 28,
data: {
username: 'kennethCodesAllDay',
joinDate: 'March 26, 2016',
organization: 'freeCodeCamp',
friends: [
'Sam',
'Kira',
'Tomo'
],
location: {
city: 'San Francisco',
state: 'CA',
country: 'USA'
}
}
};
function addFriend(userObj, friend) {
var friend = friend;
userObj.data.friends.push(friend);
var arrayF = userObj.data.friends;
return arrayF;
}
console.log(addFriend(user, 'Pete'));
//OOP PROGRAMING
//1. Constructors are functions that create new objects.
//They define properties and behaviors that will belong to the new object. example
function Dog(name, color) {
this.name = name;
this.color = color;
this.numLegs = 4;
}
var dodger = new Dog('doger', 'brown');
//instanceof allows you to compare an object to a constructor, returning true or false based on whether or not that object was created with the constructor.
dodger instanceof Dog //returns true;
//OR
dodger.constructor === Dog;
class Bird {
constructor(name) {
this.name = name;
this.numLegs = 2;
}
}
let canary = new Bird("Tweety");
let ownProps = [];
for (let p in canary) {
if (canary.hasOwnProperty(p)) {
ownProps.push(p);
}
}
ownProps //['name','numLegs']
// Use Prototype Properties to Reduce Duplicate Code
//A better way is to use Bird’s prototype.The prototype is an object that is shared among ALL instances of Bird.
// Here's how to add flightCapacity to the Bird prototype:
Bird.prototype.flightCapacity = "300feet";
//Now all instances of Bird have the flightCapacity property.
// Since all instances automatically have the properties on the prototype, think of a prototype as a "recipe" for creating objects.
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
let beagle = new Dog("Snoopy");
let ownProps = [];
let prototypeProps = [];
for (var p in beagle) {
if (beagle.hasOwnProperty(p)) {
ownProps.push(p); //['name']
//The output is as a result of the Dog construtor function contains only
// the name property on declaration and the numLegs was added through the
// prototype update which den updates all Dog instances. But the property belongs to the constructor function
} else {
prototypeProps.push(p);
}
}
// To add more property to a constructor create an object element
// Remember to add the constructor property
function Dog(name) {
this.name = name;
}
Dog.prototype = {
constructor: Dog, //*//
numLegs: 2,
eat: function () {
console.log("nom nom nom");
},
describe: function () {
console.log("My name is " + this.name);
}
};
Dog.prototype.isPrototypeOf(beagle); // return true shows Dog is the prototype of beagle
// end of file