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

জাভাস্ক্রিপ্টে পলিমরফিজম (Polymorphism) ইমপ্লিমেন্ট করার পদ্ধতি


পলিমরফিজম (Polymorphism)

পলিমরফিজম অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং (OOP)-এর মূল তত্ত্বের একটি। এটি অবজেক্টগুলোকে এমনভাবে ডিজাইন করতে সাহায্য করে যাতে সেগুলো নির্দিষ্ট প্রদত্ত অবজেক্টের সাথে আচরণ শেয়ার বা ওভাররাইড করতে পারে। পলিমরফিজম ইনহেরিটেন্স-এর সুবিধা নিয়ে এটি সাধন করে।

নিচের উদাহরণে চাইল্ড অবজেক্টগুলো যেমন 'cricket' এবং 'tennis' প্যারেন্ট অবজেক্ট 'game'-এর 'select' মেথড ওভাররাইড করে নये স্ট্রিং রিটার্ন করেছে আউটপুটে দেখানো মতে। অন্যদিকে, অন্য চাইল্ড অবজেক্ট 'football' select মেথড ওভাররাইড না করে শেয়ার (ইনহেরিট) করেছে এবং প্যারেন্ট স্ট্রিং আউটপুট হিসেবে দেখাচ্ছে।

উদাহরণ

<html> <body> <script>    var game = function () {}       game.prototype.select = function()    {       return " i love games and sports"    }    var cricket = function() {}    cricket.prototype = Object.create(game.prototype);    cricket.prototype.select = function()                //  overridden the select method to display      {                                                             new string.         return "i love cricket"    }    var tennis = function() {}    tennis.prototype = Object.create(game.prototype);  // overridden the select method to display new    tennis.prototype.select = function()                  string                  {       return "i love tennis"    }    var football = function() {}    football.prototype = Object.create(game.prototype);  // shared parent property    var games = [new game(), new cricket(), new tennis(), new football()];    games.forEach(function(game){       document.write(game.select());     document.write("</br>");    }); </script> </body> </html>

আউটপুট

i love games and sports i love cricket i love tennis i love games and sports

  1. অপেরা ব্রাউজারে জাভাস্ক্রিপ্ট এনেবল করার পদ্ধতি

  2. জাভাস্ক্রিপ্টে মেথড বরো করা (call, apply, bind)

  3. জাভাস্ক্রিপ্টে অ্যারে রিডিউস করা (reduce)

  4. জাভাস্ক্রিপ্টে অ্যাসিনক্রোনাস লুপ বাস্তবায়ন