• 【javascript】简单原型链、借用构造函数


    javascript中继承(实现继承)的方式--简单原型链

      1 .简单原型链事例

       function Super() { };
            Super.prototype = {
                name:"Super",
                sayName:function () {
                    alert(this.name);
                }
            }
            
            function Sub() {
                //Sub.prototype.name = "sub";
            }

            Sub.prototype = new Super();

            var sub1 = new Sub();
            sub1.sayName();//提示Super

       2. 简单原型链和借用构造函数事例

        function Super(name,age) {
                this.name = name;
                this.age = age;
                this.color = ["red", "blue"];
            };
            Super.prototype = {
                sayName:function () {
                    alert(this.name);
                }
            }
            
            function Sub(name,age) {
                Super.call(this,name,age);
            }

            Sub.prototype = new Super();
            Sub.prototype.sayAge = function () {
                alert(this.age);
            }

            var sub1 = new Sub("sub1",1);
            sub1.sayName();//sub1
            sub1.sayAge();//1
            sub1.color.push("green");
            alert(sub1.color); //red,blue,green

            var sub2 = new Sub("sub2", 2);
            sub2.sayName();//sub2
            sub2.sayAge();//2
            alert(sub2.color); //red,blue

  • 相关阅读:
    使用StoryBoard执行动画
    导航控制器里边添加UIScrollView (automaticallyAdjustsScrollViewInsets)
    IOS中手势UIGestureRecognizer
    已知UIScrollView放大后的Frame和放大之前的Frame计算放大的瞄点坐标
    UIScrollView 在手指点击的坐标处放大
    [ZOJ3899]State Reversing
    多项式操作汇总
    [BZOJ4916]神犇和蒟蒻
    [BZOJ4176]Lucas的数论
    [BZOJ3309]DZY Loves Math
  • 原文地址:https://www.cnblogs.com/zspbolg/p/3517109.html
Copyright © 2020-2023  润新知