_javascript technique of prototype chain inheritance instance of JS object inheritance

Source: Internet
Author: User

The example of this article describes the usage of the prototype chain inheritance of JS object inheritance. Share to everyone for your reference. The specific analysis is as follows:

Copy Code code as follows:
<script type= "Text/javascript" >
Define the object of the cat
var kitty = {color: ' Yellow ', bark:function () {alert (' Meow meow ');},climb:function () {alert (' I'll climb the Tree ')}};

The constructor of the Tiger object
Function Tiger () {
This.color = "Yellow and black";
This.back = function () {
Alert (' Roar ... ');
}
}

To declare a stereotype to a constructor, the object that is constructed will have an ancestor: the prototype
Tiger.prototype = Kitty;
or tiger.prototype = new Kitty ()//If Kitty is a function, use this method

var t = new Tiger ();
document.write (T.color);
T.CLIMB ()///When calling the Tiger's property or method, first in its constructor, or, if not, the prototype of the Tiger constructor. Note, however, that it does not copy the Climb () method from the prototype object to itself. This is the prototype chain lookup.
</script>


Other explanation: Kitty also has a construction method, the new Object (). Object defaults to some methods and properties, see "Object" in the JavaScript manual. At the same time, it also has a prototype, just for empty {}.

I hope this article will help you with your JavaScript programming.

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.