அம்பு செயல்பாடுகள் சிறியவை, ஆனால் உண்மையான வேறுபாடுகள் பிணைப்பு நடத்தையில் உள்ளன, வெறுமனே வாக்கியறிவில் மட்டுமல்ல.
const regular = function () {};
const arrow = () => {};
const short = x => x * 2; // implicit return for one expression
அம்பு செயல்பாடுகள் சிறியவை, ஆனால் உண்மையான வேறுபாடுகள் பிணைப்பு நடத்தையில் உள்ளன, வெறுமனே வாக்கியறிவில் மட்டுமல்ல.
const regular = function () {};
const arrow = () => {};
const short = x => x * 2; // implicit return for one expression
1. சொந்த this இல்லை — அம்புகள் சுற்றியுள்ள நோக்கத்திலிருந்து this ஐ உரிமைப்பெறும். இது மிக முக்கியமான வேறுபாடு மற்றும் அவை callbacks க்கு ஏன் சிறந்தவை:
const timer = {
seconds: 0,
start() {
setInterval(() => this.seconds++, 1000); // ✅ `this` is timer
// a regular function here would have its own `this` (undefined) → bug
},
};
2. arguments பொருள் இல்லை — பதிலாக rest parameters ஐ பயன்படுத்தவும்:
const sum = (...args) => args.reduce((a, b) => a + b, 0);
3. Constructors ஆக பயன்படுத்த முடியாது — new arrow() வீசி விடுகிறது, மற்றும் அவற்றிற்கு prototype இல்லை.
const obj = {
name: "Ann",
greet: () => `Hi ${this.name}`, // ❌ `this` is NOT obj (it's outer scope)
greet2() { return `Hi ${this.name}`; }, // ✅ method needs dynamic `this`
};
object methods க்கு அம்பு பயன்படுத்த வேண்டாம் (அவை பொருளுக்கு பிணைக்கப்படாது) அல்லது நீங்கள் dynamic this/arguments தேவைப்படும் போது (உறுப்பு event handlers அது this இருக்கும் நம்பியுள்ளது).
Callbacks மற்றும் குறுகிய செயல்பாடுகளுக்கு அம்புகளை பயன்படுத்தவும் இங்கு this ஐ உரிமைப்பெறுவது நீங்கள் விரும்புவது; methods, constructors, மற்றும் prototype methods க்கு வழக்கமான செயல்பாடுகளைப் பயன்படுத்தவும்.