Photo by Andrew Neel on Unsplash
JavaScript 2022 - New Features - Part 2
ECMAScript 2022(ES13) : New Capabilities
Class State Initialization
The class state can be initialized during declaration
class Emp {
eno=100;
ename="ram";
constructor() {
}
};
let obj = new Emp();
console.log(obj.eno,obj.ename); // 100 ram
In the above code the state of Emp is initialized during declaration i.e fields eno and ename are initialized to default values. Any object created of Emp will have the state.
Static Class Fields
The static keyword can be used with fields. The fields can be initialized during declaration.
// static field
class Myclass {
static eno;
static name="ram";
static show() {
console.log(Myclass.eno, Myclass.name); // undefined ram
}
};
Myclass.show();
console.log(Myclass.eno, Myclass.name); // undefined ram
In the above code the static keyword is used with field and function. The static field is accessed using class name.The static field has common state for all the objects.
Private access
Using the symbol # private access can be enforced. The programmer can make the following private
field
function
The private access can also be enforced for static elements.
// Private access
class Myclass2 {
#data=100;
static #data2=200;
#show() {
console.log("private data", this.#data, Myclass2.#data2); // 100 200
console.log("")
}
show2() {
this.#show();
}
};
let obj2 = new Myclass2();
obj2.show2();
In the above code the private accessibility is enforced using # symbol. The element has to be prefixed with # symbol to make it private.
Static fields and Inheritance behaviour
The static fields can be inherited if they are public.
// static in inheritance
class MC {
static mcField = "Base";
static show() {
console.log(MC.mcField);
}
};
class CC extends MC {
static ccField = "Subclass";
};
console.log(MC.mcField); // Base
console.log(CC.mcField); // Base
console.log(CC.ccField); // Subclass
MC.show();// Base
In the above code the fields of MC class can be accessed in the CC class.
Static Block
A class can contain static block which can be used for initializing static fields.
// static block
class Myclass3 {
static i; // cannot declare 2 variables
static j;
static {
Myclass3.i=100;
Myclass3.j=200;
console.log("Static block - init");
}
static show() {
console.log(Myclass3.i,Myclass3.j); // 100 200
}
};
Myclass3.show();
In the above code the static block is used to initialize the static fields.
Complete Code Listing
class Emp {
eno=100;
ename="ram";
constructor() {
}
};
let obj = new Emp();
console.log(obj.eno,obj.ename); // 100 ram
// static field
class Myclass {
static eno;
static name="ram";
static show() {
console.log(Myclass.eno, Myclass.name); // undefined ram
}
};
Myclass.show();
console.log(Myclass.eno, Myclass.name); // undefined ram
// Private access
class Myclass2 {
#data=100;
static #data2=200;
#show() {
console.log("private data", this.#data, Myclass2.#data2); // 100 200
console.log("")
}
show2() {
this.#show();
}
};
let obj2 = new Myclass2();
obj2.show2();
// static in inheritance
class MC {
static mcField = "Base";
static show() {
console.log(MC.mcField);
}
};
class CC extends MC {
static ccField = "Subclass";
};
console.log(MC.mcField); // Base
console.log(CC.mcField); // Base
console.log(CC.ccField); // Subclass
MC.show();// Base
// static block
class Myclass3 {
static i; // cannot declare 2 variables
static j;
static {
Myclass3.i=100;
Myclass3.j=200;
console.log("Static block - init");
}
static show() {
console.log(Myclass3.i,Myclass3.j); // 100 200
}
};
Myclass3.show();