Skip to content

Instantly share code, notes, and snippets.

@ShilpiMaurya

ShilpiMaurya/index.js

Last active Aug 22, 2019
Embed
What would you like to do?
object oriented programming in js
// Try edit message
// Object oriented prograaming: 4 principles
// 1. Encapsulation
// 2. Inheritance
// 3. Composition
// 4. Polymorphism
//encapsulation
class Rectangle {
constructor(length, width){
this.length = length;
this.width = width;
}
printArea(){
console.log('area is: ', this.length * this.width);
}apis
printParameter(){
console.log('parameter is: ', 2(this.length + this.width));
}
}
// inheritance
class Square extends Rectangle{
constructor(side){
super(side, side);
}
}
class NewSquare{
constructor(side){
//composition
this.myRectangle = new Rectangle(side, side);
}
printArea(){
this.myRectangle.printArea();
}
printParameter(){
this.myRectangle.printParameter();
}
}
// consumer
const myRectangle = new Rectangle(2, 3);
myRectangle.printArea();
const mySquare = new Square(2);
mySquare.printArea();
const myNewSquare = new NewSquare(2);
myNewSquare.printArea();
// polymorphism
function add(arg1, arg2){
return arg1 + arg2;
}
console.log(add(1,2));
console.log(add("1","2"));
console.log(add("abc","def"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.