The example in this article describes the use of JavaScript to define attributes to an object by prototype. Share to everyone for your reference. The specific analysis is as follows:
The following JS code defines the movie object. In the process of using the object and through the prototype to the object to add the Iscomedy attribute, the invocation of the direct use of object.iscomedy can be very convenient.
<script type= "Text/javascript" >
<!--
function movietostring () {return
(title: "+this.title+") Director: "+this.director);
}
function Movie (title, director) {
this.title = title;
This.director = Director | | "Unknown"; If null assign to ' unknown '
this.tostring = movietostring;//assign function to this method pointer
}
var o Fficespace = new Movie ("Officespace");
var narnia = new Movie ("Narnia", "Andrew Adamson");
Movie.prototype.isComedy = false; Add a field to the movie ' s prototype
document.write (narnia.tostring ());
document.write ("<br/>narnia a comedy?" +narnia.iscomedy);
Officespace.iscomedy = true; Override the default just for this object
document.write ("<br/>office spaces a comedy?" +officespace.iscomedy);
-->
</script>
I hope this article will help you with your JavaScript programming.