Using Prototype to implement object-oriented JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to use Prototype to implement object-oriented methods in JavaScript. examples show how to use Prototype attributes, which is of great practical value, for more information about how to use Prototype to implement object-oriented JavaScript, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

Prototype is an attribute of a Function object. It points to another object. All attributes and methods of this object will be inherited by the constructor instance.

At the same time, prototype has a constructor reference pointing to the constructor. In this way, a prototype chain structure of circular reference is successfully formed.

We can directly define the unchanged attributes and methods on the prototype object to save memory overhead.

Function Cat (name, color) {this. name = name; this. color = color;} Cat. prototype. type = 'mammal'; Cat. prototype. eat = function () {console. log ('eat fish ') ;}; var cat1 = new Cat ('Kitty', 'white'); var cat2 = new Cat ('smokey', 'black '); console. log (cat1.type); // mammalconsole. log (cat1.eta === cat2.eta); // TRUE, same referenceconsole. log (cat1.constructor === Cat) // TRUE, from Person. prototype

I hope this article will help you design javascript programs.

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.