Home > Web Front-end > JS Tutorial > How to use prototype property

How to use prototype property

青灯夜游
Release: 2021-04-21 15:09:59
Original
3037 people have browsed it

The prototype attribute is used to add attributes and methods to objects. The syntax for using this attribute is "object.prototype.name=value".

How to use prototype property

The operating environment of this article: Windows 7 system, javascript version 1.8.5, Dell G3 computer.

Each constructor has an attribute called prototype, which is used to add properties and methods to the object. The following article will introduce you to the prototype attribute. I hope it will be helpful to you.

javascript prototype property

The prototype property gives you the ability to add properties and methods to an object.

Basic syntax of prototype attribute:

object.prototype.name=value
Copy after login

javascript Example of using prototype attribute

Below Let’s look at specific examples

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function employee(name, jobtitle, born) {
  this.name = name;
  this.jobtitle = jobtitle;
  this.born = born;
}
employee.prototype.salary = 6000;
var fred = new employee("小华", "工程师", 1985);
document.getElementById("demo").innerHTML = fred.name+","+fred.jobtitle+",薪水为:"+fred.salary;
</script>
</body>
</html>
Copy after login

Renderings:

How to use prototype property

The above is the detailed content of How to use prototype property. For more information, please follow other related articles on the PHP Chinese website!

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