Home > Web Front-end > JS Tutorial > JavaScript defines an object through function and adds toString() method to the object. Example analysis_javascript skills

JavaScript defines an object through function and adds toString() method to the object. Example analysis_javascript skills

WBOY
Release: 2016-05-16 16:08:06
Original
967 people have browsed it

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>
Copy after login

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template