
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to use the 'with' keyword in JavaScript?
The with keyword is used as a kind of shorthand for referencing an object's properties or methods.
The object specified as an argument to with becomes the default object for the duration of the block that follows. The properties and methods for the object can be used without naming the object.
Syntax
The syntax for with object is as follows −
with (object){ properties used without the object name and dot }
Example
You can try to learn the following code to learn how to implement with keyword −
<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>
Output
Book title is : Python Book author is : Tutorialspoint Book price is : 100
- Related Articles
- How to use the debugger keyword in JavaScript?
- How to use void keyword in JavaScript?
- How to use 'const' keyword in JavaScript?
- How to use the "defined?" keyword in Ruby?
- How to use the "not" keyword in Ruby?
- How to use the "or" keyword in Ruby?
- How to use the readonly keyword in TypeScript?
- How to use the 'and' keyword in Ruby?
- How to use typeof with arguments in JavaScript?
- How does the “this” keyword work in JavaScript?
- How can I use the arithmetic operators (+,-,*,/) with unit values of INTERVAL keyword in MySQL?
- How to use media queries with JavaScript?
- How to use JavaScript to replace the content of a document with JavaScript?
- How MySQL behaves if I use INTERVAL keyword with an invalid date?
- How can I use INTERVAL keyword with MySQL NOW() and CURDATE() functions?

Advertisements