如何在 JavaScript 中向对象添加属性和方法?
htmljavascriptprogramming scripts更新于 2024/6/5 20:28:17
要在 JavaScript 中向对象添加属性和方法,请使用 prototype 属性。
示例
您可以尝试运行以下代码来了解如何使用原型 −
<html>
<head>
<title>JavaScript prototype property</title>
<script>
function book(title, author) {
this.title = title;
this.author = author;
}
</script>
</head>
<body>
<script>
var myBook = new book("Amit", "Java");
book.prototype.price = null;
myBook.price = 500;
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>
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

