This article mainly introduces the JavaScript through function definition object and adds ToString () method to the object, the example analyzes the function definition object in JavaScript and the use skill of adding method, has certain reference value, Need friends can refer to the following
The example of this article analyzes JavaScript to define objects through function and add ToString () method to objects. Share to everyone for your reference. The specific analysis is as follows:
The following JS code defines a movie object through a function, defines a tostring method within the movie object, and the ToString method is implemented by an external function.
?
1 2 3 4 5 6 7 8 9 10 11-12 |
<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 help you with your JavaScript programming.