Class and Object
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(Hi, I'm ${this.name} and I'm ${this.age} years old.);
}
}
const p = new Person(“Alice”, 25);
p.greet(); // Output: Hi, I’m Alice and I’m 25 years old.
Static Object
class Student {
constructor(name, grade) {
this.name = name;
this.grade = grade;
}
// Regular instance method
info() {
console.log(${this.name} is in grade ${this.grade});
}
// Static property (belongs to the class itself, not instances)
static schoolInfo = {
name: “Greenwood High”,
location: “City Center”
};
}
// Create a new Student instance
const s1 = new Student(“John”, 8);
// Call the instance method
s1.info(); // Output: John is in grade 8
// Access the static property (using the class name)
console.log(Student.schoolInfo);
// Output: { name: “Greenwood High”, location: “City Center” }
Async
settimeout, read file etc…
Promises
function getData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(“Data received”);
}, 2000);
});
}
getData().then(data => {
console.log(data);
});
///////////////////////////////////////////////////////////////
async function fetchData() {
const result = await getData();
console.log(result);
}
fetchData();
Async Await
async function getUser() {
const response = await fetch(“https://jsonplaceholder.typicode.com/users/1”);
const data = await response.json();
console.log(data.name);
}
getUser();