From 45b2058b9660ec7f567d91209f574a45626b7e42 Mon Sep 17 00:00:00 2001 From: rodrysauco <32721337+rodrysauco@users.noreply.github.com> Date: Wed, 27 Jun 2018 21:29:02 -0300 Subject: [PATCH 1/3] Adding initial structure --- 02-oop-inheritance/Models/EventEmitter.js | 13 +++++++++++++ 02-oop-inheritance/Models/Movie.js | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 02-oop-inheritance/Models/EventEmitter.js create mode 100644 02-oop-inheritance/Models/Movie.js diff --git a/02-oop-inheritance/Models/EventEmitter.js b/02-oop-inheritance/Models/EventEmitter.js new file mode 100644 index 000000000..211b89e84 --- /dev/null +++ b/02-oop-inheritance/Models/EventEmitter.js @@ -0,0 +1,13 @@ +class EventEmitter{ + + on(){ + + } + + emit(){ + + } + off(){ + + } +} \ No newline at end of file diff --git a/02-oop-inheritance/Models/Movie.js b/02-oop-inheritance/Models/Movie.js new file mode 100644 index 000000000..569fdd295 --- /dev/null +++ b/02-oop-inheritance/Models/Movie.js @@ -0,0 +1,17 @@ +class Movie{ + + constructor(title,year,duration){ + this.title = title; + this.year = year; + this.duration = duration; + } + play(){ + return "play"; + } + resume(){ + return "resume"; + } + pause(){ + return "pause"; + } +} \ No newline at end of file From 08ab4f52b935861290a5fe9eb2c820ff51b3d974 Mon Sep 17 00:00:00 2001 From: Rodry Date: Tue, 3 Jul 2018 00:02:45 -0300 Subject: [PATCH 2/3] Begining EX 9 Splited all clases --- 02-oop-inheritance/JS/Models/Actor.js | 6 + 02-oop-inheritance/JS/Models/EventEmitter.js | 59 ++++++++++ 02-oop-inheritance/JS/Models/Logger.js | 5 + 02-oop-inheritance/JS/Models/Movie.js | 29 +++++ 02-oop-inheritance/JS/Models/Social.js | 8 ++ 02-oop-inheritance/JS/beforeSpliting.js | 116 +++++++++++++++++++ 02-oop-inheritance/JS/playing.js | 7 ++ 02-oop-inheritance/Models/EventEmitter.js | 13 --- 02-oop-inheritance/Models/Movie.js | 17 --- 02-oop-inheritance/index.html | 20 ++++ 10 files changed, 250 insertions(+), 30 deletions(-) create mode 100644 02-oop-inheritance/JS/Models/Actor.js create mode 100644 02-oop-inheritance/JS/Models/EventEmitter.js create mode 100644 02-oop-inheritance/JS/Models/Logger.js create mode 100644 02-oop-inheritance/JS/Models/Movie.js create mode 100644 02-oop-inheritance/JS/Models/Social.js create mode 100644 02-oop-inheritance/JS/beforeSpliting.js create mode 100644 02-oop-inheritance/JS/playing.js delete mode 100644 02-oop-inheritance/Models/EventEmitter.js delete mode 100644 02-oop-inheritance/Models/Movie.js create mode 100644 02-oop-inheritance/index.html diff --git a/02-oop-inheritance/JS/Models/Actor.js b/02-oop-inheritance/JS/Models/Actor.js new file mode 100644 index 000000000..7e65c9fa2 --- /dev/null +++ b/02-oop-inheritance/JS/Models/Actor.js @@ -0,0 +1,6 @@ +export class Actor{ + constructor(name,age){ + this.name = name; + this.age = age; + } +}; diff --git a/02-oop-inheritance/JS/Models/EventEmitter.js b/02-oop-inheritance/JS/Models/EventEmitter.js new file mode 100644 index 000000000..b0b705875 --- /dev/null +++ b/02-oop-inheritance/JS/Models/EventEmitter.js @@ -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"); + } + } +} \ No newline at end of file diff --git a/02-oop-inheritance/JS/Models/Logger.js b/02-oop-inheritance/JS/Models/Logger.js new file mode 100644 index 000000000..d4e98429d --- /dev/null +++ b/02-oop-inheritance/JS/Models/Logger.js @@ -0,0 +1,5 @@ +export default class Logger{ + log(info){ + console.log('The '+ info +' event has been emited'); + } +} \ No newline at end of file diff --git a/02-oop-inheritance/JS/Models/Movie.js b/02-oop-inheritance/JS/Models/Movie.js new file mode 100644 index 000000000..721b9c73d --- /dev/null +++ b/02-oop-inheritance/JS/Models/Movie.js @@ -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); + } + } + +} \ No newline at end of file diff --git a/02-oop-inheritance/JS/Models/Social.js b/02-oop-inheritance/JS/Models/Social.js new file mode 100644 index 000000000..502a81c4e --- /dev/null +++ b/02-oop-inheritance/JS/Models/Social.js @@ -0,0 +1,8 @@ +export default let social = { + share: function(friendName){ + console.log(`${friendName} shares ${this.title}`); + }, + like: function(friendName){ + console.log(`${friendName} likes ${this.title}`); + } +}; \ No newline at end of file diff --git a/02-oop-inheritance/JS/beforeSpliting.js b/02-oop-inheritance/JS/beforeSpliting.js new file mode 100644 index 000000000..2b81d40a8 --- /dev/null +++ b/02-oop-inheritance/JS/beforeSpliting.js @@ -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); \ No newline at end of file diff --git a/02-oop-inheritance/JS/playing.js b/02-oop-inheritance/JS/playing.js new file mode 100644 index 000000000..33c307d69 --- /dev/null +++ b/02-oop-inheritance/JS/playing.js @@ -0,0 +1,7 @@ +import A from './Actor.js' +import movie from './Movie.js'; +import logger from './Logger.js'; +import social from './Social.js'; + +let actor = new Actor('Robert de Niro',60); +console.log(actor.name); \ No newline at end of file diff --git a/02-oop-inheritance/Models/EventEmitter.js b/02-oop-inheritance/Models/EventEmitter.js deleted file mode 100644 index 211b89e84..000000000 --- a/02-oop-inheritance/Models/EventEmitter.js +++ /dev/null @@ -1,13 +0,0 @@ -class EventEmitter{ - - on(){ - - } - - emit(){ - - } - off(){ - - } -} \ No newline at end of file diff --git a/02-oop-inheritance/Models/Movie.js b/02-oop-inheritance/Models/Movie.js deleted file mode 100644 index 569fdd295..000000000 --- a/02-oop-inheritance/Models/Movie.js +++ /dev/null @@ -1,17 +0,0 @@ -class Movie{ - - constructor(title,year,duration){ - this.title = title; - this.year = year; - this.duration = duration; - } - play(){ - return "play"; - } - resume(){ - return "resume"; - } - pause(){ - return "pause"; - } -} \ No newline at end of file diff --git a/02-oop-inheritance/index.html b/02-oop-inheritance/index.html new file mode 100644 index 000000000..b4d8e6f81 --- /dev/null +++ b/02-oop-inheritance/index.html @@ -0,0 +1,20 @@ + + + + Topic 2 + + + + +
+ Playing with OOP +
+ + \ No newline at end of file From f2a115b59326534df45215d94d43255c4a235cda Mon Sep 17 00:00:00 2001 From: Rodry Date: Tue, 3 Jul 2018 22:42:20 -0300 Subject: [PATCH 3/3] Last commit Already to pull request --- .gitignore | 1 + 02-oop-inheritance/.babelrc | 3 + 02-oop-inheritance/.empty | 0 02-oop-inheritance/JS/Models/Actor.js | 2 +- 02-oop-inheritance/JS/Models/Social.js | 4 +- 02-oop-inheritance/JS/compiledClasses.js | 213 +++ 02-oop-inheritance/index.html | 2 +- 02-oop-inheritance/package-lock.json | 2169 ++++++++++++++++++++++ 02-oop-inheritance/package.json | 14 + 9 files changed, 2404 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 02-oop-inheritance/.babelrc delete mode 100644 02-oop-inheritance/.empty create mode 100644 02-oop-inheritance/JS/compiledClasses.js create mode 100644 02-oop-inheritance/package-lock.json create mode 100644 02-oop-inheritance/package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..46205175a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/02-oop-inheritance/node_modules \ No newline at end of file diff --git a/02-oop-inheritance/.babelrc b/02-oop-inheritance/.babelrc new file mode 100644 index 000000000..fa8c9a047 --- /dev/null +++ b/02-oop-inheritance/.babelrc @@ -0,0 +1,3 @@ +{ + "presets":["es2015"] +} \ No newline at end of file diff --git a/02-oop-inheritance/.empty b/02-oop-inheritance/.empty deleted file mode 100644 index e69de29bb..000000000 diff --git a/02-oop-inheritance/JS/Models/Actor.js b/02-oop-inheritance/JS/Models/Actor.js index 7e65c9fa2..9432efdba 100644 --- a/02-oop-inheritance/JS/Models/Actor.js +++ b/02-oop-inheritance/JS/Models/Actor.js @@ -3,4 +3,4 @@ export class Actor{ this.name = name; this.age = age; } -}; +} diff --git a/02-oop-inheritance/JS/Models/Social.js b/02-oop-inheritance/JS/Models/Social.js index 502a81c4e..2eaea9d2c 100644 --- a/02-oop-inheritance/JS/Models/Social.js +++ b/02-oop-inheritance/JS/Models/Social.js @@ -1,8 +1,8 @@ -export default let social = { +export let social = { share: function(friendName){ console.log(`${friendName} shares ${this.title}`); }, like: function(friendName){ console.log(`${friendName} likes ${this.title}`); } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/02-oop-inheritance/JS/compiledClasses.js b/02-oop-inheritance/JS/compiledClasses.js new file mode 100644 index 000000000..4b9f6102d --- /dev/null +++ b/02-oop-inheritance/JS/compiledClasses.js @@ -0,0 +1,213 @@ +"use strict"; + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +var Actor = function Actor(name, age) { + _classCallCheck(this, Actor); + + this.name = name; + this.age = age; +}; +"use strict"; + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +var EventEmitter = function () { + function EventEmitter() { + _classCallCheck(this, EventEmitter); + + this.events = {}; + } + //Receives the name of the event and a function + + + _createClass(EventEmitter, [{ + key: "on", + value: 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; + + }, { + key: "emit", + value: function emit(nameEvent) { + if (this.events[nameEvent]) { + var event = this.events[nameEvent]; + //The arrow let us create a one line function in this case + if (event.length > 0) { + event.forEach(function (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"); + } + } + }, { + key: "off", + value: function 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 + var 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"); + } + } + }]); + + return EventEmitter; +}(); + +'use strict'; + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +var Logger = function () { + function Logger() { + _classCallCheck(this, Logger); + } + + _createClass(Logger, [{ + key: 'log', + value: function log(info) { + console.log('The ' + info + ' event has been emited'); + } + }]); + + return Logger; +}(); + +'use strict'; + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +var Movie = function (_EventEmitter) { + _inherits(Movie, _EventEmitter); + + function Movie(title, year, duration) { + _classCallCheck(this, Movie); + + var _this = _possibleConstructorReturn(this, (Movie.__proto__ || Object.getPrototypeOf(Movie)).call(this)); + + _this.title = title; + _this.year = year; + _this.duration = duration; + _this.casting = []; + return _this; + } + + _createClass(Movie, [{ + key: 'play', + value: function play() { + this.emit('play'); + } + }, { + key: 'pause', + value: function pause() { + this.emit('pause'); + } + }, { + key: 'resume', + value: function resume() { + this.emit('resume'); + } + }, { + key: 'addCast', + value: function addCast(actor) { + var _this2 = this; + + if (Array.isArray(actor)) { + actor.forEach(function (a) { + return _this2.casting.push(a); + }); + } else { + this.casting.push(actor); + } + } + }]); + + return Movie; +}(EventEmitter); + +"use strict"; + +var social = { + share: function share(friendName) { + console.log(friendName + " shares " + this.title); + }, + like: function like(friendName) { + console.log(friendName + " likes " + this.title); + } +}; + +let deadPool = new Movie("DeadPool",2015,109); +Object.assign(deadPool,social); +let logger = new Logger(); +deadPool.on('play',logger.log); +deadPool.on('pause',logger.log); +deadPool.on('resume',logger.log); +deadPool.off('resume',logger.log); +deadPool.off('foo',logger.log); //This will fail +deadPool.play(); +deadPool.pause(); +//We erased the function asociated with this key before +deadPool.resume(); +console.log(deadPool.events); + +let actor = new Actor('Ryan Reynolds',41); +let otherCast = [ + new Actor("Ed Skrein",35), + new Actor("T. J. Miller",37), + new Actor("Morena Baccarin",39), + new Actor("Gina Carano",36), + new Actor("Leslie Uggams",75), + new Actor("Brianna Hildebrand",21), + new Actor("Stefan Kapicic",39) +]; +deadPool.addCast(otherCast); +deadPool.addCast(actor); +console.log(deadPool.casting); + +deadPool.share("Ariel Ganduglia"); +deadPool.like("Sebastian de la Fuente"); +deadPool.share("Javier Constanzo"); \ No newline at end of file diff --git a/02-oop-inheritance/index.html b/02-oop-inheritance/index.html index b4d8e6f81..9650bd64d 100644 --- a/02-oop-inheritance/index.html +++ b/02-oop-inheritance/index.html @@ -2,7 +2,7 @@ Topic 2 - +