Answers for "4. You want to print the incremental count each time you instantiate a object using new in JS"

0

4. You want to print the incremental count each time you instantiate a object using new in JS

class Key {
    // The static property
    static lastKey = 0;
    
    // The instance property using the class fields proposal syntax
    // Note I didn't initialize it with 1, that's a bit misleading.
    key;
    
    constructor() {
        // Increment and assign
        this.key = ++Key.lastKey;
    }

    print_key() {
        console.log(this.key)
    }
}

const key1 = new Key();
const key2 = new Key();
const key3 = new Key();

key1.print_key();
key2.print_key();
key3.print_key();
Posted by: Guest on September-20-2020

Code answers related to "4. You want to print the incremental count each time you instantiate a object using new in JS"

Code answers related to "Javascript"

Browse Popular Code Answers by Language