浅谈js的继承

先对js的几个特殊属性与函数做点解释:
caller:每个函数(方法)都有的属性,可知是由谁调用此方法。
call: 每个函数都有的方法,可调用父构造函数(调用一个对象的一个方法,以另一个对象替换当前对象(其实就是更改对象的内部指针,即改变对象的this指向的内容))。
prototype:js对象均有的一个属性,也称原型属性

<html>
<head>
<script type="text/javascript">
    /**
    动物,父类
    */
    function Animal(name)
    {
        this.name = name;
        this.sleep = function ()
        {
            alert("name is :" + this.name +" ,\r\n it's a sleep function 。\r\n the caller is:" + this.sleep.caller);
        }
     }
     /**
         子类,老虎
    */
    function Tiger() 欠款
    {
        var name = "tiger_onedear";
        Animal.call(this , name);   //将animal对象作为this指针调用tiger函数,调用父构造函数。换句话说就是tiger继承animal。
    }
    /**
        子类,小鸟
    */
    function Bird()
    {
        var name = "bird_onedear";
    }
    Bird.prototype=new Animal("bird_onedear"); //建一个积累的对象作为子类原型的原型(原型继承),个人认为也可以勉强说是bird继承animal
    Bird.prototype.fly= function () {alert("i am fly" );} //对bird对象原型增加一个方法,这样bird与bird的子类都能用上
   
    function testTiger()
    {
        new Tiger().sleep();  //tiger类定义处并没有sleep方法,但由于继承了animal类,所以子类调用正常
    }
   
    function testBird()
    {
        new Bird().sleep();
        new Bird().fly();
    }   
</script>tbw淘宝网
</head>
<body>
<input type="button" onclick="testTiger();" value="TtestTigeriger"/>
<input type="button" onclick="testBird();" value="testBird"/>
</body>
</html>


原文地址:https://www.cnblogs.com/sky7034/p/2154080.html