zoukankan      html  css  js  c++  java
  • 浅谈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>


  • 相关阅读:
    MySQL主键和外键使用及说明
    SQLAlchemy
    路飞学城购买流程API
    路飞学城知识点
    使用rest_framework写api接口的一些注意事项(axios发送ajax请求)
    微信推送功能
    支付宝支付业务
    路飞学城前端Vue
    Python爬虫,用第三方库解决下载网页中文本的问题
    Python爬虫,抓取淘宝商品评论内容
  • 原文地址:https://www.cnblogs.com/sky7034/p/2154080.html
Copyright © 2011-2022 走看看