SoFunction
Updated on 2025-02-28

JavaScript defines attribute usage examples for objects through prototype

This article describes the usage of JavaScript defining attributes to objects through prototype. Share it for your reference. The specific analysis is as follows:

The following JS code defines the movie object. During the process of using the object, the isComedy property is added to the object through prototype, which can be used directly when calling, which is very convenient.

<script type="text/javascript">
<!--
function movieToString() {
  return("title: "++" director: "+);
}
function movie(title, director) {
   = title;
   = director || "unknown"; //if null assign to "unknown"
   = movieToString; //assign function to this method pointer
}
var officeSpace = new movie("OfficeSpace");
var narnia = new movie("Narnia","Andrew Adamson");
 = false; //add a field to the movie's prototype
(());
("<br />Narnia a comedy? "+);
 = true; //override the default just for this object
("<br />Office Space a comedy? "+);
//-->
</script>

I hope this article will be helpful to everyone's JavaScript programming.