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

জাভাস্ক্রিপ্টে 'with' কীওয়ার্ড ব্যবহার করার পদ্ধতি

'with' কীওয়ার্ডটি একটি অবজেক্টের প্রপার্টি বা মেথড রেফারেন্স করার জন্য শর্টহ্যান্ড হিসেবে ব্যবহৃত হয়।

with-এ আর্গুমেন্ট হিসেবে নির্দিষ্ট অবজেক্টটি পরবর্তি ব্লকের সময়কালের জন্য ডিফল্ট অবজেক্ট হয়ে যায়। অবজেক্টের প্রপার্টি এবং মেথডগুলো অবজেক্টের নাম ছাড়া ব্যবহার করা যায়।

সিনট্যাক্স

with অবজেক্টের সিনট্যাক্স নিম্নরূপ:

with (object){    properties used without the object name and dot }

উদাহরণ

with কীওয়ার্ড কীভাবে ইমপ্লিমেন্ট করতে হয় তা শিখতে নিচের কোডটি দেখুন:

<html>    <head>       <title>User-defined objects</title>       <script>          // Define a function which will work as a method          function addPrice(amount){             with(this){                price = amount;             }          }          function book(title, author){             this.title = title;             this.author = author;             this.price = 0;             this.addPrice = addPrice; // Assign that method as property.          }       </script>    </head>    <body>       <script type="text/javascript">          var myBook = new book("Python", "Tutorialspoint");          myBook.addPrice(100);          document.write("Book title is : " + myBook.title + "<br>");          document.write("Book author is : " + myBook.author + "<br>");          document.write("Book price is : " + myBook.price + "<br>");       </script>    </body> </html>

আউটপুট

Book title is : Python Book author is : Tutorialspoint Book price is : 100
  1. জাভাস্ক্রিপ্ট ব্যবহার করে ক্যানভাসে ড্র করার পদ্ধতি

  2. জাভাস্ক্রিপ্ট দিয়ে টেক্সটের রঙ কীভাবে রিটার্ন করবেন?

  3. জাভাস্ক্রিপ্টে Object.defineProperty ব্যবহার করা

  4. জাভাস্ক্রিপ্টে অবজেক্টের দৈর্ঘ্য (length) কীভাবে পেতে হয়