2
Mon Apr 07 2025 00:57:36 GMT+0000 (Coordinated Universal Time)
Saved by
@sem
Prototypal Inheritance and Classes
function Person(name) {
this.name = name;
}
Person.prototype.sayHi = function () {
console.log("Hi, I'm " + this.name);
};
class Student extends Person {
constructor(name, grade) {
super(name);
this.grade = grade;
}
showGrade() {
console.log(`${this.name}'s grade is ${this.grade}`);
}
}
const s = new Student("Roshini", "A");
s.sayHi();
s.showGrade();
Object and Array Destructuring
const user = { name: "Roshini", age: 20 };
const { name, age } = user;
console.log(name, age);
const arr = [10, 20];
const [a, b] = arr;
console.log(a, b);
Modules
Steps:
Create math.js:
export function add(x, y) {
return x + y;
}
Create main.js:
import { add } from "./math.js";
console.log(add(5, 3));
Add <script type="module" src="main.js"></script> in HTML
Function Generators and Symbols
function* countUp() {
yield 1;
yield 2;
yield 3;
}
let gen = countUp();
console.log(gen.next().value);
const sym1 = Symbol("id");
console.log(sym1 === Symbol("id")); // false
Closure
function outer() {
let count = 0;
return function () {
count++;
console.log("Count:", count);
};
}
const counter = outer();
counter(); // 1
counter(); // 2
content_copyCOPY
Comments