-
-
Save SirSerje/1a2f89a44a0e177e1213ff8dc2165779 to your computer and use it in GitHub Desktop.
2 - Object Oriented Programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Counter { | |
constructor(i = 0) { | |
// this.counter = 999 | |
this._count = i | |
} | |
addOne() { | |
this._count++ | |
} | |
getCounter () { | |
return this._count | |
} | |
setCounter(i) { | |
if(typeof i !== 'number') { | |
throw new Error('new counter value is not a number') | |
} | |
this._count = i; | |
} | |
get counter() { | |
return this._count | |
} | |
set counter(i) { | |
console.log(i) | |
if(typeof i !== 'number') { | |
throw new Error('new counter value is not a number') | |
} | |
this._count = i; | |
} | |
double() { | |
this._count = this._count * 2 | |
return this._count | |
} | |
} | |
const a = new Counter(3) | |
a.counter = 10 | |
console.log(a.counter) | |
// a.addOne() | |
// a.setCounter(3) | |
// a.addOne() | |
// a.addOne() | |
// a.count = 0 | |
// console.log(a.count) | |
// a.double() | |
// console.log(a.getCounter()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//another counter | |
//approach will be discussed on next lesson | |
function Counter(i=0) { | |
let count = 0 | |
function increase() { | |
count++ | |
} | |
function decrease() { | |
count-- | |
} | |
function getCount() { | |
return count | |
} | |
return ({ | |
increase, | |
decrease, | |
getCount | |
}) | |
} | |
const c = Counter() | |
c.increase() | |
c.increase() | |
c.decrease() | |
console.log(c.getCount()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rectangle { | |
constructor(width, height) { | |
this._width = width; | |
this._height = height | |
} | |
getArea() { | |
return this._width * this._height | |
} | |
} | |
class Square { | |
constructor(width) { | |
this._width = width | |
} | |
getArea() { | |
return this._width * this._width | |
} | |
} | |
class Circle { | |
constructor(radius) { | |
this._radius = radius | |
} | |
getArea() { | |
return Math.PI * this._radius ^ 2 | |
} | |
} | |
class AreaCalc { | |
constructor() { | |
this._figures = [] | |
} | |
addFigure(figure) { | |
if (!figure) { | |
return | |
} | |
this._figures.push(figure) | |
} | |
getFigures() { | |
return this._figures | |
} | |
area() { | |
const result = this._figures.reduce((acc, i) => { | |
if (i.getArea && typeof i.getArea === 'function') { | |
return acc + i.getArea() | |
} else { | |
return acc | |
} | |
}, 0) | |
return result | |
} | |
} | |
class Triangle { | |
constructor(a, b, c) { | |
this._a = a; | |
this._b = b; | |
this._c = c; | |
this.getArea = 5 | |
} | |
} | |
const rect = new Rectangle(2, 5) | |
const square = new Square(3) | |
const circle = new Circle(2) | |
const triangle = new Triangle(1, 2, 3) | |
const calc = new AreaCalc() | |
calc.addFigure(rect) | |
calc.addFigure(square) | |
calc.addFigure(circle) | |
calc.addFigure(triangle) | |
console.log(calc.area()) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//abstraction & inheritance | |
class AbstractFigure { | |
getType() { | |
'Geometric Figure' | |
} | |
getArea() { | |
return 0 | |
} | |
render() { | |
throw new Error('cant use abstract method') | |
} | |
} | |
// const aF = new AbstractFigure() | |
class Rectangle extends AbstractFigure { | |
constructor(width, height) { | |
super() | |
this._width = width; | |
this._height = height | |
} | |
getArea() { | |
return this._width * this._height | |
} | |
render() { | |
console.log('reactangle on screen') | |
} | |
} | |
class Square extends AbstractFigure{ | |
constructor(width) { | |
super() | |
this._width = width | |
} | |
getArea() { | |
return this._width * this._width | |
} | |
render() { | |
console.log('square on screen') | |
} | |
} | |
class Circle extends AbstractFigure { | |
constructor(radius) { | |
super() | |
this._radius = radius | |
} | |
getArea() { | |
return Math.PI * this._radius ^ 2 | |
} | |
} | |
class AreaCalc { | |
constructor() { | |
this._figures = [] | |
} | |
addFigure(figure) { | |
if (!figure) { | |
return | |
} | |
this._figures.push(figure) | |
} | |
getFigures() { | |
return this._figures | |
} | |
area() { | |
const result = this._figures.reduce((acc, i) => { | |
return acc + i.getArea() | |
}, 0) | |
return result | |
} | |
} | |
class Triangle extends AbstractFigure { | |
constructor(a, b, c) { | |
super() | |
this._a = a; | |
this._b = b; | |
this._c = c; | |
} | |
} | |
class Display { | |
constructor(items) { | |
this._items = items; | |
this._items.forEach(i => i.render()) | |
} | |
} | |
const rect = new Rectangle(2, 5) | |
const square = new Square(3) | |
const circle = new Circle(2) | |
const triangle = new Triangle(1, 2, 3) | |
const calc = new AreaCalc() | |
calc.addFigure(rect) | |
calc.addFigure(square) | |
calc.addFigure(circle) | |
calc.addFigure(triangle) | |
console.log(triangle.getArea()) | |
const d = new Display([rect, square]) | |
//const d = new Display([rect, square, triangle]) //triangle will throw error | |
// console.log(calc.area()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment