Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ShilpiMaurya
Last active August 22, 2019 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShilpiMaurya/d1b957973d61afbc733e6e8fe5313bf8 to your computer and use it in GitHub Desktop.
Save ShilpiMaurya/d1b957973d61afbc733e6e8fe5313bf8 to your computer and use it in GitHub Desktop.
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