Home > Web Front-end > JS Tutorial > body text

JavaScript中的原型prototype属性使用详解_基础知识

WBOY
Release: 2016-05-16 15:56:25
Original
998 people have browsed it

 prototype属性可以将属性和方法添加到任何对象(Number, Boolean, String 和Date等)。

注:原型(Prototype)是一个全局的属性,它可以使用在几乎所有的对象。
语法

object.prototype.name = value

Copy after login

实例:

这里有一个例子展示了如何使用原型(prototype)属性的属性添加到对象:

<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

这将产生以下结果:

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

Copy after login

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!