prototype/ call/ apply/ slice.call/ this/ 研究研究,太灵活了,不好掌握

 <script>
        function a() { this.b = "b"; }
        function b() { this.a = "a" }
        function c() { this.c = "c"; }
        b.prototype = new a();//prototype 可以向其原型动态添加功能(方法和属性)。b继承a,b的constructor 被改变为a,那么当new b的时候new 的是哪个constructor?a的还是b的? b的,prototype 保存的只是b函数的一个引用,不是b原型本身,而是b原型的一部分(prototype在javascript中是所有function的一个属性,里面记载着function和function的constructor),
        //b.prototype.constructor = b; 
        //b extend a, constructor change a;
        c.prototype = new b();
        //c.prototype.constructor = c;
        var c1 = new c();
        document.writeln(c.prototype.constructor);

       c.call(a);//相当于a继承c ,另一种说法就是把c 函数的 this=a了(c 函数中的this被指向 a 函数)。

//上面这局做了三件事,一 : 初始化了c, 二: 替换this指向,////三:调用c。


    </script>

例子---

<script>
        function a() { 
            this.a = "a";
            this.show = function () { alert(this + this.a + "dddd"); return this.a;}
            /// alert("dddd");
            alert(this);
            alert(this+" yiyyy "+this.show());
            alert(this);
        }
        function b() { this.b = "b" }
        function c() { alert(this); this.g = "c"; alert(this); alert(g); }
       // b.prototype = new a();
       // b.prototype.constructor = b;
        //b extend a, constructor change a;
        //c.prototype = new b();
        //c.prototype.constructor = c;
       // var c1 = new c();
       // a.prototype = b.prototype;
       // document.writeln(b.prototype.a);
        a.call(c);
        
     </script>
  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值