zoukankan      html  css  js  c++  java
  • 继承

    07_闭包.pdf06_继承.pdf
     
    # 继承  #
    ## 对象冒充  ## 
        让父类的构造函数成为子类的方法,然后调用该子类的方法,通过this关键字给所有的属性和方法赋值。
        call 和 apply 方法: 改变函数内部的函数上下文 this,使它指向传入函数的具体对象。


    **示例**:

        父对象:
        
        function Parent(name,age){
            this.name = name;
            this.age = age;
            this.SayName = function(){
                console.log(this.name);
            }
        }
        Parent.prototype.SayAge=function(){
            console.log(this.age);
        };

        子对象:
        
        function Child(name,age){
            this.obj = Parent; // 使父类的构造函数成为子类的方法
            this.obj(name,age); // 调用该子类的方法
            delete this.obj;
            Parent.call(this,name,age);// 其中 this 指 Child .功能和上面三行一样,都是对象冒充
            Parent.apply(this,[name,age]);//功能同上 apply用法和call用法都一样,只不过参数里面要加中括号
        }
        var a = new Child("zs",1);
        a.SayName();
        a.Sayage();// no function 可以使用Parent里面定义的属性,但是不能使用原型链定义的属性,这个不能访问


    **原型链的方式** 
    使子类原型对象指向父类的实例以实现继承,即重写类的原型。


        父对象
        function Parent(name,age){
            this.name = name;
            this.age = age;
            this.SayName = function(){
                console.log(this.name);
            }
        }
        Parent.prototype.SayAge=function(){
            console.log(this.age);
        };

        子对象 下面是通过原型链实现继承 只有单继承
        
        function Child(name,age,sex){
            this.sex = sex ;
            //通过父类的构造器对属性进行初始化
            this.constructor(name,age); // 用了下面一句之后里面的constructor指的是Parent的
            //如果不用上面一句,下面执行值为:undefined
        }
        Child.prototype=new Parent(); // 子类通过原型的方式获得父类对象中的属性
        var a = new Child("lisi",2,3);
        console.log(a.name); // 父类对象 lishi
        console.log(a.age); // 父类对象age 2
        console.log(a.sex); // Child 里自带的属性sex 3
        a.SayName(); //父类对象SayName() lishi
        a.SayAge(); // 2 可以访问 Parent 的 prototype 里设置的属性


    **混合call和原型链**

        父对象
        function Parent(name,age){
            this.name = name;
            this.age = age;
            this.SayName = function(){
                console.log(this.name);
            }
        }
        Parent.prototype.SayAge=function(){
            console.log(this.age);
        };

        子对象:
        function Child(name,age){   
            Parent.call(this,name,age);// 其中 this 指 Child .功能和上面三行一样,都是对象冒充
           }
        Child.prototype=new Parent(); // 子类通过原型的方式获得父类对象中的属性
        var a = new Child("lisi",2,3);
        a.SayName();


    ## 闭包 ##
       延长变量的作用域 注: 所有对象本身就是一个闭包。
     
       1 函数作为返回值:局部变量在函数执行完成之后未被销毁

        function fuc(){
            var max = 20;
            return function bar (x){
                if(x > max){
                    console.log(x);
                }
            }
        }
        var fuc1 = fuc();
        fuc1(25); // 值:25

       2 函数作为参数被传递 注:自由变量 max 在函数声明时已经确定了其作用域。

        var max = 10 ;
        var fn = function(x){
            // 确定了max的作用域,max的值为10;
            if(x > max){
                console.log(x);
            }
        };
        // 匿名函数
        (function(a){
            var max = 20;
            a(11);
        })(fn); // 11

     
  • 相关阅读:
    Java HttpClient使用小结
    【剑指offer】Q18:树的子结构
    poj3041-Asteroids , 二分图的最小顶点覆盖数 = 最大匹配数
    jquery.validate+jquery.form提交的三种方式
    "undefined reference to" 问题解决方法
    [Oracle]
    Effective_java之二:慎用重载函数
    C99规范
    迭代、递归替代循环
    1)Linux程序设计入门--基础知识
  • 原文地址:https://www.cnblogs.com/muqnly/p/4824463.html
Copyright © 2011-2022 走看看