Skip to content

Commit 74b3b2d

Browse files
committed
added js oop problems
1 parent 407976c commit 74b3b2d

File tree

6 files changed

+186
-0
lines changed

6 files changed

+186
-0
lines changed

csbin/oop/01-makePerson.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function makePerson(name, age) {
2+
return {
3+
name,
4+
age,
5+
}
6+
}
7+
8+
const vicky = makePerson('Vicky', 24);
9+
console.log(vicky.name); // -> Logs 'Vicky'
10+
console.log(vicky.age); // -> Logs 24

csbin/oop/02-Object.create.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*** CHALLENGE 2 ***/
2+
3+
const personStore = {
4+
greet() {
5+
console.log("hello");
6+
}
7+
};
8+
9+
// /********* Uncomment this line to test your work! *********/
10+
// personStore.greet(); // -> Logs 'hello'
11+
12+
13+
14+
/*** CHALLENGE 3 ***/
15+
16+
function personFromPersonStore(name, age) {
17+
const person = Object.create(personStore);
18+
person.name = name;
19+
person.age = age;
20+
return person;
21+
}
22+
23+
const sandra = personFromPersonStore('Sandra', 26);
24+
25+
26+
// /********* Uncomment these lines to test your work! *********/
27+
// console.log(sandra.name); // -> Logs 'Sandra'
28+
// console.log(sandra.age); //-> Logs 26
29+
// sandra.greet(); //-> Logs 'hello'
30+
31+
32+
33+
/*** CHALLENGE 4 ***/
34+
35+
// add code here
36+
personStore.introduce = function() {
37+
console.log(`Hi, my name is ${this.name}`)
38+
}
39+
40+
sandra.introduce(); // -> Logs 'Hi, my name is Sandra'

csbin/oop/03-new-keyword.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*** CHALLENGE 5 ***/
2+
3+
function PersonConstructor() {
4+
this.greet = function() {
5+
console.log('"hello');
6+
}
7+
}
8+
9+
10+
// /********* Uncomment this line to test your work! *********/
11+
const simon = new PersonConstructor;
12+
// simon.greet(); // -> Logs 'hello'
13+
14+
15+
16+
/*** CHALLENGE 6 ***/
17+
18+
function personFromConstructor(name, age) {
19+
const person = new PersonConstructor();
20+
person.name = name;
21+
person.age = age;
22+
return person;
23+
}
24+
25+
const mike = personFromConstructor('Mike', 30);
26+
27+
28+
// /********* Uncomment these lines to test your work! *********/
29+
// console.log(mike.name); // -> Logs 'Mike'
30+
// console.log(mike.age); //-> Logs 30
31+
// mike.greet(); //-> Logs 'hello'
32+
33+
34+
35+
/*** CHALLENGE 7 ***/
36+
// add code here
37+
PersonConstructor.prototype.introduce = function() {
38+
console.log(`Hi, my name is ${this.name}`);
39+
}
40+
41+
mike.introduce(); // -> Logs 'Hi, my name is Mike'

csbin/oop/04-es6-classes.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*** CHALLENGE 8 ***/
2+
3+
class PersonClass {
4+
constructor(name) {
5+
this.name = name;
6+
}
7+
8+
greet() {
9+
console.log("hello")
10+
}
11+
12+
}
13+
14+
15+
// /********* Uncomment this line to test your work! *********/
16+
const george = new PersonClass;
17+
// george.greet(); // -> Logs 'hello'
18+
19+
20+
21+
/*** CHALLENGE 9 ***/
22+
23+
class DeveloperClass extends PersonClass {
24+
introduce() {
25+
console.log(`Hello World, my name is ${this.name}`);
26+
}
27+
}
28+
29+
// /********* Uncomment these lines to test your work! *********/
30+
const thai = new DeveloperClass('Thai', 32);
31+
console.log(thai.name); // -> Logs 'Thai'
32+
thai.introduce(); //-> Logs 'Hello World, my name is Thai'

csbin/oop/05-subclassing.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const userFunctionStore = {
2+
sayType: function() {
3+
console.log("I am a " + this.type);
4+
}
5+
}
6+
7+
function userFactory(name, score) {
8+
let user = Object.create(userFunctionStore);
9+
user.type = "User";
10+
user.name = name;
11+
user.score = score;
12+
return user;
13+
}
14+
15+
/*** CHALLENGE 10 ***/
16+
17+
const adminFunctionStore = {
18+
// add code here
19+
__proto__: userFunctionStore
20+
}
21+
22+
/*** CHALLENGE 11, 12, 13 ***/
23+
24+
function adminFactory(name, score) {
25+
const admin = userFactory(name, score);
26+
admin.type = "Admin";
27+
Object.setPrototypeOf(admin, adminFunctionStore);
28+
return admin;
29+
}
30+
31+
/*** CHALLENGE 14 ***/
32+
/* Put code here for a method called sharePublicMessage*/
33+
adminFunctionStore.sharePublicMessage = function() {
34+
console.log("Welcome users!");
35+
}
36+
37+
const adminFromFactory = adminFactory("Eva", 5);
38+
39+
// /********* Uncomment these lines to test your work! *********/
40+
adminFromFactory.sayType() // -> Logs "I am a Admin"
41+
adminFromFactory.sharePublicMessage() // -> Logs "Welcome users!"

csbin/oop/06-mixins.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Dog {
2+
constructor() {
3+
this.legs = 4;
4+
}
5+
speak() {
6+
console.log('Woof!');
7+
}
8+
}
9+
10+
const robotMixin = {
11+
skin: 'metal',
12+
speak: function() { console.log(`I have ${this.legs} legs and am made of ${this.skin}`) },
13+
}
14+
15+
let robotFido = new Dog();
16+
17+
// robotFido = /* Put code here to give Fido robot skills */;
18+
robotFido = Object.assign(robotFido, robotMixin);
19+
20+
21+
// /********* Uncomment to test your work! *********/
22+
robotFido.speak() // -> Logs "I am made of metal"

0 commit comments

Comments
 (0)