Adanced methods Flashcards

(6 cards)

1
Q

Class and Object

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Static Object

A

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” }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Async

A

settimeout, read file etc…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Promises

A

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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Async Await

A

async function getUser() {
const response = await fetch(“https://jsonplaceholder.typicode.com/users/1”);
const data = await response.json();
console.log(data.name);
}

getUser();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly