Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/02-oop-inheritance/node_modules
3 changes: 3 additions & 0 deletions 02-oop-inheritance/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets":["es2015"]
}
Empty file removed 02-oop-inheritance/.empty
Empty file.
6 changes: 6 additions & 0 deletions 02-oop-inheritance/JS/Models/Actor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class Actor{
constructor(name,age){
this.name = name;
this.age = age;
}
}
59 changes: 59 additions & 0 deletions 02-oop-inheritance/JS/Models/EventEmitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export default class EventEmitter{
constructor(){
this.events = {};
}
//Receives the name of the event and a function
on(nameEvent, func){
//We search the event in the object
if(this.events[nameEvent]){
//We look if the event already has the function
if(this.events[nameEvent].indexOf(func) != -1){
console.log("This function was loaded before");
}else{
//We add the function to the event
this.events[nameEvent].push(func);
}
}else{
//We create an array and assign the function to it.
this.events[nameEvent] = [func];
}
}
//Receives the name of the event;
emit(nameEvent){
if (this.events[nameEvent]) {
let event = this.events[nameEvent];
//The arrow let us create a one line function in this case
if (event.length > 0) {
event.forEach(fn => { // => : fn of X (function{})
if (fn) {
//If the function exists, we call it;
fn(nameEvent);
} else {
console.log("The function doesnt exists");
}
});
} else {
console.log("Event without a function");
}
} else {
console.log("Event undefined");
}
}

off(nameEvent, func){
//We receive the event and the function
if(this.events[nameEvent]){
//We look for the event
if(this.events[nameEvent].length > 0){
//If the event has more than one function
let idx = this.events[nameEvent].indexOf(func);
//We remove the item of the array
this.events[nameEvent].splice(idx,1);
} else {
delete this.events[nameEvent];
}
} else {
console.log("Event not found");
}
}
}
5 changes: 5 additions & 0 deletions 02-oop-inheritance/JS/Models/Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default class Logger{
log(info){
console.log('The '+ info +' event has been emited');
}
}
29 changes: 29 additions & 0 deletions 02-oop-inheritance/JS/Models/Movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import EventEmitter from "./EventEmitter.js";

export default class Movie extends EventEmitter{
constructor(title,year,duration){
super();
this.title = title;
this.year = year;
this.duration = duration;
this.casting = [];
}
play(){
this.emit('play');
}
pause(){
this.emit('pause');
}
resume(){
this.emit('resume');
}

addCast(actor){
if (Array.isArray(actor)) {
actor.forEach(a => this.casting.push(a));
} else {
this.casting.push(actor);
}
}

}
8 changes: 8 additions & 0 deletions 02-oop-inheritance/JS/Models/Social.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export let social = {
share: function(friendName){
console.log(`${friendName} shares ${this.title}`);
},
like: function(friendName){
console.log(`${friendName} likes ${this.title}`);
}
}
116 changes: 116 additions & 0 deletions 02-oop-inheritance/JS/beforeSpliting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
class EventEmitter{
constructor(){
this.events = {};
}
//Receives the name of the event and a function
on(nameEvent, func){
//We search the event in the object
if(this.events[nameEvent]){
//We look if the event already has the function
if(this.events[nameEvent].indexOf(func) != -1){
console.log("This function was loaded before");
}else{
//We add the function to the event
this.events[nameEvent].push(func);
}
}else{
//We create an array and assign the function to it.
this.events[nameEvent] = [func];
}
}
//Receives the name of the event;
emit(nameEvent){
if (this.events[nameEvent]) {
let event = this.events[nameEvent];
//The arrow let us create a one line function in this case
if (event.length > 0) {
event.forEach(fn => { // => : fn of X (function{})
if (fn) {
//If the function exists, we call it;
fn(nameEvent);
} else {
console.log("The function doesnt exists");
}
});
} else {
console.log("Event without a function");
}
} else {
console.log("Event undefined");
}
}

off(nameEvent, func){
//We receive the event and the function
if(this.events[nameEvent]){
//We look for the event
if(this.events[nameEvent].length > 0){
//If the event has more than one function
let idx = this.events[nameEvent].indexOf(func);
//We remove the item of the array
this.events[nameEvent].splice(idx,1);
} else {
delete this.events[nameEvent];
}
} else {
console.log("Event not found");
}
}
}

class Movie extends EventEmitter{
constructor(title,year,duration){
super();
this.title = title;
this.year = year;
this.duration = duration;
this.casting = [];
}
play(){
this.emit('play');
}
pause(){
this.emit('pause');
}
resume(){
this.emit('resume');
}

addCast(actor){
if (Array.isArray(actor)) {
actor.forEach(a => this.casting.push(a));
} else {
this.casting.push(actor);
}
}

}

class Actor{
constructor(name,age){
this.name = name;
this.age = age;
}
}

class Logger{
log(info){
console.log('The '+ info +' event has been emited');
}
}

let social = {
share: function(friendName){
console.log(`${friendName} shares ${this.title}`);
},
like: function(friendName){
console.log(`${friendName} likes ${this.title}`);
}
};

let movie = new Movie("Terminator",1990,90);
Object.assign(movie,social);
let logger = new Logger();
movie.on('play',logger.log);
movie.on('pause',logger.log);
movie.on('resume',logger.log);
Loading