This article describes the usage of prototype chain inheritance of js object inheritance. Share it for your reference. The specific analysis is as follows:
Copy the code as follows:<script type="text/javascript">
//Define the object of the cat
var kitty = {color:'yellow',bark:function(){alert('meow');},climb:function(){alert('I can climb trees')}};
//Tiger object constructor
function tiger(){
this.color = "yellow and black";
this.back = function(){
alert('roo...');
}
}
//Declare the prototype to the constructor, then the constructed object will have an ancestor: that is, the prototype
tiger.prototype = kitty;
//or tiger.prototype = new kitty();//If kitty is function, this method is used
var t = new tiger();
document.write(t.color);
t.climb();//When calling the tiger's property or method, first look for its constructor; if not, go to the prototype of the tiger constructor. But be aware that here it does not copy the climb() method in the prototype object to itself. This is prototype chain search.
</script>
Other notes: kitty also has a constructor, that is, new Object(). Object also has some methods and properties by default, see "object object" in the javascript manual. At the same time, it also has a prototype, just empty { }.
I hope this article will be helpful to everyone's JavaScript programming.