Typescript ile Class ismini dinamik olarak almak

typescript, programming, beginner, class, constructor

DALLE_2023-10-31_21.54.58_-_Hand-drawn_cute_lineal_color_illustration_deeply_saturated_in_mor_tones_showing_an_ENTP_kitten_inside_a_magical_alchemy_lab._The_kitten_wearing_a_w.png
S8MdhNw.png

getClassName Yaklasimi

  • Her seferinde tekrardan isim dǒndururlur

class Base {
    getClassName() {
        return this.constructor.name;
    }
}

class Derived extends Base {}

const derivedInstance = new Derived();
console.log(derivedInstance.getClassName());  // "Derived" çıktısını verir

uMLmKw3.png

readonly className Yaklasimi

  • Tek seferlik atama yapilir ve memoryde tutulur

class Base {
    readonly className = this.constructor.name;
}

class Derived extends Base {}

const baseInstance = new Base();
console.log(baseInstance.className);  // "Base" çıktısını verir

const derivedInstance = new Derived();
console.log(derivedInstance.className);  // "Derived" çıktısını verir

Last updated

Was this helpful?