Home > Web Front-end > JS Tutorial > Detailed explanation of the use of prototype attribute in JavaScript_Basic knowledge

Detailed explanation of the use of prototype attribute in JavaScript_Basic knowledge

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:56:25
Original
1093 people have browsed it

The prototype attribute can add properties and methods to any object (Number, Boolean, String and Date, etc.).

Note: Prototype is a global property that can be used in almost all objects.
Grammar

object.prototype.name = value

Copy after login

Example:

Here is an example showing how to add properties to an object using the prototype attribute:

<html>
<head>
<title>User-defined objects</title>

<script type="text/javascript">

function book(title, author){
  this.title = title; 
  this.author = author;
}
</script>
</head>
<body>
<script type="text/javascript">
  var myBook = new book("Perl", "Mohtashim");
  book.prototype.price = null;
  myBook.price = 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>

Copy after login

This will produce the following results:

Book title is : Perl
Book author is : Mohtashim
Book price is : 100

Copy after login

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template