0

Javascript ES6 - is it possible to use/call super class properties or functions without using this keyword in child class

class Parent {

    constructor() {
        this.strings = 'some string values'
        this.utils = 'some util functions'
    }
}

class Child extends Parent {
    constructor() {
        super()

    }

    login() {
        //currently i am using like this to access parent class property or functions
        console.log(this.strings.success)

        //but i would like to use like this 
        console.log(strings.success)


    }
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Rajesh Wolf
  • 1,005
  • 8
  • 12

1 Answers1

2

You can destructure this, and assign the properties to their own variables/constants:

class Parent {

  constructor() {
    this.strings = { success: 'some string values' }
    this.utils = 'some util functions'
  }
}

class Child extends Parent {
  login() {
    const { strings } = this;

    console.log(strings.success)
  }
}

const child = new Child

child.login()
Ori Drori
  • 183,571
  • 29
  • 224
  • 209