কম্পিউটার টিউটোরিয়াল

জাভাস্ক্রিপ্ট this আইডেন্টিফায়ার


জাভাস্ক্রিপ্ট this কীওয়ার্ড সেটি যেসব অবজেক্টের অন্তর্গত সেটি রেফারেন্স করে। এটি একক থাকলে বা ফাংশনের ভিতরে গ্লোবাল অবজেক্টকে রেফার করে। মেথডের ভিতরে থাকলে ওনার অবজেক্টকে রেফার করে এবং ইভেন্ট লিসেনারের ভিতরে ইভেন্ট পাওয়া HTML এলিমেন্টকে রেফার করে।

উদাহরণ

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style>    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample {       font-size: 18px;       font-weight: 500;       color: red;    } </style> </head> <body> <h1>JavaScript this Identifier</h1> <div class="sample"></div> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3> Click on the above button to see which object 'this' refers to in multiple context </h3> <script>    let thisRef = this;    let sampleEle = document.querySelector(".sample");    function test() {       return this;    }    let testObj = {       a: 22,       check() {          return this;       },    };    document.querySelector(".Btn").addEventListener(       "click",       () => {          sampleEle.innerHTML ="This inside normal function = " + test() + "<br>";          sampleEle.innerHTML +="This inside a method = " + testObj.check() + "<br>";          sampleEle.innerHTML += "This without any scope = " + thisRef + "<br>";       },       false    ); </script> </body> </html>

আউটপুট

জাভাস্ক্রিপ্ট this আইডেন্টিফায়ার

'CLICK HERE' বাটনে ক্লিক করলে -

জাভাস্ক্রিপ্ট this আইডেন্টিফায়ার


  1. জাভাস্ক্রিপ্ট Random - ম্যাথ.র‍্যান্ডম()

  2. জাভাস্ক্রিপ্ট Promises - অ্যাসিঙ্ক্রোনাস অপারেশন

  3. জাভাস্ক্রিপ্ট WeakSet

  4. জাভাস্ক্রিপ্ট this কীওয়ার্ড ব্যাখ্যা