DAY4

作者: 小可_34e0 | 来源:发表于2019-11-10 23:20 被阅读0次

原型对象的应用 扩展内置对象方法

<script >
        //通过原型对象,对原来的内置对象进行扩展自定义方法,比如给数组增加自定义求偶数和的功能

        console.log(Array.prototype);
        Array.prototype.sum=function(){
            var sum=0;
            for(var i=0;i<this.length;i++){
                sum+=this[i];
            }
            return sum;
        }
        var arr=[1,2,3];
        console.log(arr.sum());
                console.log(Array.prototype);
                var arr1=new Array(11,22,33);
        console.log(arr1.sum());
    </script>

注意!!数组和字符内置对象不能给原型对象覆盖操作Array.prototype={},只能是Array.prototype.xxx=function(){}的方式

继承:
call():调用这个函数,并且修改函数运行时this的指向
fun.call(thisArg ,arg1,arg2,...)
thisArg:当前调用函数this的指向

<script >
        //call
        //1.可以调用函数
        //
        function fn(x,y){
            console.log("我想吃东西");
            console.log(this);
            console.log(x+y);
        }
        var o={
            name:'andy';
        };
        //fn();
        //1.fn.call();----调用函数
        //2.call()  可以改变这个函数的this指向,此时这个函数的this就指向o这个函数
        fn.call(o,1,2);
    </script>
运行结果

借用构造函数继承父类型函数:
核心原理:通过call()把父类的this指向子类的this,这样 就可以实现子类型继承父类型的属性

<script >
        //借用夫构造函数继承属性
        //1.父构造函数
        function Father(uname,age){
            //this 指向父构造函数的对象实例
            this.uname=uname;
            this.age=age;
        }
        //2.子构造函数
        function Son(uname,age,score){
            //this指向子构造函数的对象实例
            Father.call(this,uname,age);//调用父构造函数,把父构造函数的this指向子构造函数的this
            this.score=score;

        }
        var son=new Son('刘德华',30,100);
        console.log(son);
    </script>
运行结果

借用原型对象继承父类型方法

<script >
        //借用夫构造函数继承属性
        //1.父构造函数
        function Father(uname,age){
            //this 指向父构造函数的对象实例
            this.uname=uname;
            this.age=age;
        }
        Father.prototype.money=function(){
            console.log(100000);
        };
        //2.子构造函数
        function Son(uname,age,score){
            //this指向子构造函数的对象实例
            Father.call(this,uname,age);//调用父构造函数,把父构造函数的this指向子构造函数的this
            this.score=score;

        }
        //son.prototype=Father.prototype;-----z这样直接赋值会出现问题,如果修改了子原型对象,父原型对象也会跟着改变
        son.prototype=new Father();
        //r如果利用对象的形式修改了原型对象,别忘了利用constructor指向原来的 原型对象
        son.prototype.constructor=Son;
        //这个时子构造函数专门的方法
        son.prototype.exam=function(){
            console.log("孩子要考试");
        };
        var son=new Son('刘德华',30,100);
        console.log(son);
    </script>

es6通过类实现面向对象编程
1.类的本质其实还是一个函数,我们可以认为类就是构造函数的另外一种写法:
//es6之前通过 构造函数+原型 实现面向对象编程
//(1)构造函数有原型对象prototype
//(2)构造函数原型对象prototype里面有construct 指向构造函数本身
//(3)构造函数可以通过原型对象来添加方法
//(4)构造函数创建的实例对象有proto原型指向构造函数的原型对象

图片发布于简书app

es6中新增的方法:
数组方法:
迭代(遍历)方法:forEac()、map()、filter()、some()、every()
array.forEach(function (currentValue,index,arr))
currentValue:数组当前项的值
index:数组当前的索引
arr:数组对象本身

网友评论

      本文标题:DAY4

      本文链接:https://www.haomeiwen.com/subject/ysakbctx.html