Tech TutorBangla Web Documentation
Portfolio
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • JavaScript Documentation48টি অধ্যায়
প্রযুক্তি নির্বাচন
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • Introduction
  • JavaScript কী?
  • Syntax & Comments
  • Variables
  • Data Types: Basics
  • Data Types: Conversion
  • Data Types: Advanced
  • Strings
  • Numbers & Math
  • Date & Time
  • Operators
  • Conditions
  • Loops
  • Functions: Basics
  • Functions: Intermediate
  • Functions: Advanced
  • Scope & Hoisting
  • Closures & this
  • ES6+ Modern
  • Arrays: Basics
  • Arrays: Intermediate
  • Arrays: Advanced
  • Objects: Basics
  • Objects: Intermediate
  • Objects: Advanced
  • Class: Basics
  • Class: Intermediate
  • Class: Advanced
  • DOM: Basics
  • DOM: Intermediate
  • DOM: Advanced
  • Events: Basics
  • Events: Intermediate
  • Events: Advanced
  • Async JavaScript
  • Promise
  • Async / Await
  • Fetch API
  • JSON
  • Modules
  • Browser Storage
  • Event Loop Deep
  • Prototype Chain
  • Iterators & Generators
  • WeakMap, WeakSet, Proxy
  • Error Handling
  • Practice Problems
  • Mini Projects
  • Basics
  • Functions & Scope
  • Types & Syntax
  • Arrays & Objects
  • Advanced
  • Functional Programming
  • Objects & Symbols
  • Async Deep Dive
  • Modern JavaScript
  1. Docs
  2. JavaScript
  3. প্রশ্ন/উত্তর
  4. Advanced
  5. Q26. Explain the concept of this keyword in different contexts.
JavaScriptAdvanced

Q26. Explain the concept of this keyword in different contexts.

this কোথায় কীভাবে নির্ধারিত হয় — global, function, method, arrow আর class-এ।

this হলো ফাংশন চালানোর সময় JavaScript নির্ধারণ করে দেওয়া একটি reference — সে আগে থেকে লেখা থাকে না, ডাকার ধরনের উপর নির্ভর করে। তাই একই ফাংশন বিভিন্ন জায়গায় থেকে ডাকলে ভিন্ন this পেতে পারে।

প্রধান contextjavascript
javascript
// ১. Global context
console.log(this); // window (ব্রাউজারে)

// ২. সাধারণ function — অ-কঠোর মোডে
function show() {
  console.log(this === window); // true
}
show();

// ৩. Method — this হলো সেই object
const user = {
  name: "Rahim",
  show() {
    console.log(this.name); // "Rahim"
  },
};
user.show();

সবচেয়ে বেশি ভুল হয় যখন method টেনে আনা হয় — তখন this হারিয়ে যায়।

টেনে আনলে this হারায়javascript
javascript
const user = {
  name: "Rahim",
  show() {
    console.log(this.name);
  },
};

const fn = user.show;
fn(); // this আর user নয়

Arrow function-এর নিজস্ব this নেই — সে লেখার সময়ের বাইরের this ধার করে রাখে, তাই কোথা থেকে ডাকলেও বদলায় না।

arrow function-এর thisjavascript
javascript
const user = {
  name: "Rahim",

  arrow: () => this.name, // ধার করা this

  regular: function () {
    return this.name;
  },

  method: function () {
    // ভেতরের arrow টেনে আনার পরেও this ঠিক থাকে
    const inner = () => this.name;
    return inner();
  },
};

console.log(user.method()); // "Rahim"

Class-এর ভিতরে this সবসময় কঠোর মোডে কাজ করে, তাই call বা apply ছাড়া বসানো পরিস্থিতিতে সেটি undefined থাকে।

class-এর thisjavascript
javascript
class User {
  constructor() {
    this.name = "Rahim";
  }

  show() {
    return this.name; // new করে ডাকলে ঠিক থাকে
  }
}

const show = new User().show;
// show(); // TypeError: this is undefined

this সরাসরি ঠিক করতে call, apply বা bind ব্যবহার করা হয়।

this ঠিক করাjavascript
javascript
function show() {
  console.log(this.name);
}

const user = { name: "Rahim", show };

show.call({ name: "Karim" }); // "Karim"
show.apply({ name: "Jamal" }); // "Jamal"

const bound = show.bind({ name: "Rahim" });
bound(); // "Rahim"
Contextthis কী হয়
Globalwindow (বা undefined কঠোর মোডে)
সাধারণ functionwindow / globalThis
Object methodসেই object
Arrow functionলেখার সময়ের বাইরের this
Class methodনিজের object, নাহলে undefined
call / apply / bindওগুলোতে দেওয়া object
সতর্কতা

this খুব context-নির্ভর, তাই ভুল পথে গেলে চুপচাপ undefined বা ভুল object পাওয়া যায়। ফাংশন পাঠানোর আগে bind করুন, অথবা class method ব্যবহার করুন।

শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
Advanced বিভাগের আরও প্রশ্নJavaScript প্রশ্ন/উত্তর
Tech Tutor

বাংলায় ওয়েব ডেভেলপমেন্ট ডকুমেন্টেশন শিখুন — ধাপে ধাপে।

Homeসব ডকুমেন্টেশনQuick Startআমার নোটস

© 2026 Tech Tutor. বাংলায় শিখুন, বাংলায় গড়ুন।

mdanamolhasan1998@gmail.com