What are the rules to be followed for Object Definitions in JavaScript?

HTMLJavascriptProgramming Scripts

The following are the rules to be followed for object definitions in JavaScript −

  • Quotes are to be added to strings, for example,
var myBook = new book("Java", "John");
  • You need to place the closing bracket on a new line.

  • You need to add the opening bracket in the same line of the object name placement.

  • Between property and its value, add a colon and space.

  • End object definition with (;)semicolon.

Let’s see an example now showing how to correctly work with objects in JavaScript −

Example

 Live Demo

<html>
<head>
<title>JavaScript Array Object</title>
<script>
function book(title, author) {
this.title = title;
this.author = author;
}
</script>
</head>

<body>
<script>
var myBook = new book("Java", "John");
book.prototype.price = null;
myBook.price = 300;

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>
raja
Published on 26-Feb-2018 14:26:33
Advertisements