JavaScript defines an object through function and adds an toString of method instance analysis to the object

  • 2020-05-17 04:49:43
  • OfStack

This article analyzes the example of JavaScript defining objects through function and adding toString() methods to objects. Share with you for your reference. The specific analysis is as follows:

The JS code below defines an movie object via function, an toString method within the movie object, and an toString method via an external function.

<script type="text/javascript">
function movieToString() {
  return("title: "+this.title+" director: "+this.director);
function movie(title, director) {
  this.title = title;
  this.director = director;
  this.toString = movieToString; //assign function to this method pointer
  var aliens = new movie("Aliens","Cameron");

I hope this article is helpful for you to design javascript program.

Related articles: