Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript es6 class

class MyClass {
  constructor() {
    this.answer = 42;
  }
}

const obj = new MyClass();
obj.answer; // 42
Comment

es6 class

//class in es6 are just functional constructor.
class PersonES6{
  constructor(firstname,lastname,age){
    this.firstname= firstname;
    this.lastname=lastname;
    this.age=age
  }
  aboutPerson(){
  console.log(`My name is ${this.firstname} ${this.lastname} and I am ${this.age} years old`)
  }
}

const shirshakES6= new Person('Shirshak','Kandel',25)
shirshakES6.aboutPerson();
Comment

es6 class example

var Polygon = class { 
   constructor(height, width) { 
      this.height = height; 
      this.width = width; 
   } 
}
Comment

Classes in ES6

class Point {
    constructor(x, y) {
        this.x = x
        this.y = y
    }

    toString() {
        return '[X=' + this.x + ', Y=' + this.y + ']'
    }
}

class ColorPoint extends Point {
    static default() {
        return new ColorPoint(0, 0, 'black')
    }

    constructor(x, y, color) {
        super(x, y)
        this.color = color
    }

    toString() {
        return '[X=' + this.x + ', Y=' + this.y + ', color=' + this.color + ']'
    }
}

console.log('The first point is ' + new Point(2, 10))
console.log('The second point is ' + new ColorPoint(2, 10, 'green'))
console.log('The default color point is ' + ColorPoint.default())
Comment

class keyword es6

class Polygon {
  constructor(...sides) {
    this.sides = sides;
  }
  // Method
  *getSides() {
    for(const side of this.sides){
      yield side;
    }
  }
}

const pentagon = new Polygon(1,2,3,4,5);

console.log([...pentagon.getSides()]); // [1,2,3,4,5]
Comment

PREVIOUS NEXT
Code Example
Javascript :: split and join in node js 
Javascript :: get element by class name 
Javascript :: react html parser 
Javascript :: push values to state array class react 
Javascript :: type checking js vscode 
Javascript :: mongodb replace string 
Javascript :: unix timestamp js 
Javascript :: javascript remove element from the dom 
Javascript :: js add html element 
Javascript :: javascript speech recognition 
Javascript :: vscode js intellisence not working 
Javascript :: remove eslint check react native 
Javascript :: angular ionic capacitor nfc reader 
Javascript :: js dom siblings 
Javascript :: data table is not a function in vue 
Javascript :: how to export module in node js 
Javascript :: how to find last occurrence comma in a string and replace with value in javascript 
Javascript :: jquery option second 
Javascript :: prevstate in react 
Javascript :: object intersection javascript 
Javascript :: define an async function 
Javascript :: lodash find duplicate element index 
Javascript :: .foreach in javascript 
Javascript :: command to run nextjs project 
Javascript :: jquery values to array 
Javascript :: pm2 config changes update environments 
Javascript :: cache request in vue 
Javascript :: add and get tokens to securestore expo 
Javascript :: moment clone 
Javascript :: javascript reduce sum 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =