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 −


 Live Demo

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

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>");

Published on 26-Feb-2018 09:56:33