JavaScript · Lesson 19 / 32 · தமிழ்
JavaScript Classes
கருத்தைப் புரிந்துகொள்ளுங்கள். Code-ஐ மாற்றி இயக்குங்கள். ஒரு சிறிய பயிற்சியால் கற்றதை உறுதி செய்யுங்கள்.
மூலப் பாட விளக்கங்கள் தமிழில் உள்ளன. கூடுதல் concept, practice மற்றும் answer notes ஆங்கிலத்தில் உள்ளன.
முக்கியக் கருத்து
A class defines how related objects are created and what methods they share. The constructor initializes each instance; this refers to that instance when its method is called normally.
Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.
விரிவான விளக்கங்கள்
JavaScript-ல Classes
Classes (ES6-ல introduce ஆனது) JavaScript-ன் prototype-based inheritance-க்கு syntactic sugar. Objects create பண்றதற்கும் inheritance handle பண்றதற்கும் cleaner syntax கொடுக்குது.
class Animal {
#name;
#sound;
constructor(name, sound) {
this.#name = name;
this.#sound = sound;
}
get name() { return this.#name; }
speak() {
return `${this.#name} says ${this.#sound}!`;
}
static create(name, sound) {
return new Animal(name, sound);
}
}
const dog = new Animal("Rex", "woof");
console.log(dog.speak()); // "Rex says woof!"
// console.log(dog.#name); // ❌ Private!Inheritance
class Dog extends Animal {
#tricks = [];
constructor(name) {
super(name, "woof"); // super-ஐ முதல்ல call பண்ணணும்
}
learn(trick) {
this.#tricks.push(trick);
return this;
}
speak() {
return super.speak() + " (Good dog!)";
}
}
const buddy = new Dog("Buddy");
buddy.learn("sit").learn("shake");
console.log(buddy instanceof Dog); // true
console.log(buddy instanceof Animal); // trueமுழுமையான உதாரணம்
Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.
class Course {
constructor(title) { this.title = title; }
describe() { return "Learn " + this.title; }
}
const course = new Course("JavaScript");
console.log(course.describe());எதிர்பார்க்கும் முடிவைப் பாருங்கள்
Learn JavaScript
மாற்றி இயக்கிப் பாருங்கள்
Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.
Editor load ஆனதும் code-ஐ ஏற்றி Run அழுத்துங்கள்.
OneCompiler என்ற இலவச வெளிப்புற சேவை code-ஐ இயக்குகிறது. Run செய்யும் code அங்கு அனுப்பப்படும். தனிப்பட்ட தகவல்கள் அல்லது secret keys உள்ளிட வேண்டாம்.
சிறிய பயிற்சி
Practice challenge
Create a second Course instance for TypeScript.
விடை மற்றும் விளக்கம்
new Course("TypeScript").describe() returns "Learn TypeScript". Each instance has its own title.
தவிர்க்க வேண்டிய தவறு: Call a class constructor with new. A class is not a plain function you can invoke without it.
அடுத்த பாடத்திற்குத் தயாரா?
- Explain the example in your own words.
- Complete the challenge before revealing its answer.
- Describe the common mistake and how to avoid it.