JavaScript defines an object by function and adds ToString () method instance analysis to the object

Source: Internet
Author: User
Tags function definition

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.