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

জাভাস্ক্রিপ্টে new.target


new.target একটি মেটাপ্রোপার্টি যা রানটাইমে জানাতে সাহায্য করে কোন ফাংশন বা কনস্ট্রাক্টর new কিওয়ার্ড দিয়ে কল করা হয়েছে কিনা।

নিচে জাভাস্ক্রিপ্টে new.target-এর কোড দেওয়া হলো:

উদাহরণ

<!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;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } </style> </head> <body> <h1>জাভাস্ক্রিপ্টে new.target।</h1> <div class="result"></div> <br /> <button class="Btn">এখানে ক্লিক করুন</button> <h3>উপরোক্ত বাটনে ক্লিক করে Student কনস্ট্রাক্টর new কিওয়ার্ড ছাড়া কল করুন</h3> <script>    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    function Student(name, age, place) {       if (!new.target) {          resEle.innerHTML ="Error : Student should be used with the new operator";       }       this.name = name;       this.age = age;       this.place = place;    }    BtnEle.addEventListener("click", () => {       Student("Shawn", 22, "Delhi");    }); </script> </body> </html>

আউটপুট

জাভাস্ক্রিপ্টে new.target

‘এখানে ক্লিক করুন’ বাটনে ক্লিক করলে –

জাভাস্ক্রিপ্টে new.target


  1. জাভাস্ক্রিপ্টে continue স্টেটমেন্ট

  2. জাভাস্ক্রিপ্টে new অপারেটর

  3. জাভাস্ক্রিপ্টে debugger স্টেটমেন্ট

  4. জাভাস্ক্রিপ্টে image() অবজেক্ট