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.