আসলে, স্ট্যাটিক মেথড-এর ভিতরে সরাসরি অবজেক্ট ব্যবহার করলে ফলাফল আসে না। কিন্তু যখন অবজেক্টটি প্যারামিটার হিসেবে পাস করা হয়, তখন আমরা অবজেক্টটি অ্যাক্সেস করতে পারি। সংক্ষেপে আলোচনা করা হলো।
উদাহরণ-১
নিচের উদাহরণে, আমরা অবজেক্ট "myComp"-কে সরাসরি ব্যবহার করার চেষ্টা করেছি প্যারামিটার হিসেবে পাস না করেই। তাই কোনো ফলাফল পাওয়া যায়নি। ব্রাউজার কনসোল খুললে একটি এরর দেখাবে: "myComp.comp() is not a function"। আসল ফলাফল পেতে অবজেক্টটি প্যারামিটার হিসেবে পাস করতে হবে যেমন উদাহরণ-২-তে দেখানো হয়েছে।
<html> <body>
<p id="method"></p>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
static comp() {
return "Tutorix is the best e-learning platform";
}
}
myComp = new Company("Tesla");
document.getElementById("method").innerHTML = myComp.comp();
</script>
</body> </html>
উদাহরণ-২
নিচের উদাহরণে, অবজেক্টটি প্যারামিটার হিসেবে পাস করা হয়েছে। তাই আউটপুটে ফলাফল পাওয়া যাচ্ছে।
<html> <body>
<p id="method"></p>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
static comp(val) {
return "Elon musk is the head of " + val.name;
}
}
myComp = new Company("Tesla");
document.getElementById("method").innerHTML = Company.comp(myComp);
</script>
</body> </html>
আউটপুট
Elon musk is the head of Tesla