This article analyzes the example of defining an object through function in JavaScript and adding the toString() method to the object. Share it with everyone for your reference. The specific analysis is as follows:
The following JS code defines a movie object through function, and defines a toString method within the movie object. The toString method is implemented through 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"); document.write(aliens.toString()); </script>
I hope this article will be helpful to everyone’s JavaScript programming design.