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 :: validation input javascript 
Javascript :: Nodemailer Google Passport Oauth Strategy 
Javascript :: module imports as default 
Javascript :: nested callbacks javascript 
Javascript :: js console log function code 
Javascript :: create javascript for loop 
Javascript :: javascript Using Math.max() on an Array 
Javascript :: javascript reflect 
Javascript :: cypress check if an element is visible 
Javascript :: dynamodb json to regular json 
Javascript :: event loop javascript 
Javascript :: value js 
Javascript :: react native panresponder on click 
Javascript :: express nodejs 
Javascript :: how to add external link in angular 
Javascript :: javascript Program to check if a given year is leap year 
Javascript :: jsonl parser 
Javascript :: render html page in javascript 
Javascript :: method function difference 
Javascript :: event handler 
Javascript :: fetch composition API in Vue3 
Javascript :: convert decimal to hex 
Javascript :: javascript Insert Item to Map 
Javascript :: module.exports with JavaScript 
Javascript :: setup error handler in express framework 
Javascript :: create react app 
Javascript :: vue js props 
Javascript :: how to download array of files from aws s3 using aws sdk in nodejs 
Javascript :: how to upgrade nodejs version 
Javascript :: switch statement 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =