js继承和闭包笔记

    /* 
    继承:
        父类拥有的属性及相关方法,通过继承可以拥有(私有化的不能获取)
    继承的实现方式:
        1.es6的extends关键词来实现继承(class)
    */
    class Person {
        constructor() {
            this.name = "傻狗"
        }
    }
    class Son extends Person {
        constructor() {
            super()//指向父类的构造函数 如果要使用this关键词
            this.age = 218
        }
    }
    var son = new Son()
    console.log(son.name, son.age);

    /*
        2.原型继承(将对应的需要继承在对象加在原型上)
     */
    function Person1() {
        this.name = "嘟嘟嘟"
    }
    function Son1() {
        this.age = 18
    }
    //原型继承 将对应的继承对象 赋值给对应的原型
    Son1.prototype = new Person1()
    var son1 = new Son1()
    console.log(son1.name, son1.age);

    /*
        3.通过this来指向继承
    */
    function Person2() {
        this.name = "大傻狗"
    }
    function Son2() {
        //this 指向对应的son来实例的对象
        Person2.call(this)//更改对应Person的this指向 指向son实例对象
        this.age = 18
    }
    Son2.prototype = new Person2()
    var son2 = new Son2()
    console.log(son2.name, son2.age);

    /* 
    闭包(closure)指有权限访问另一个函数作用域中变量的函数
    闭包的概念:
        1.在函数内部返回函数
        2.函数内部要存储对应外部函数内的引用
        3.那么这个数据就不会被回收(持久化)
    优点:
        1.持久化 可以作为缓存
        2.不会造成数据的全局污染(外部的内容不会直接访问函数的变量)
    缺点:
        1.因为不会被回收 那么内存空间会一直占用
        2.会一直保存引用
     */
    function fu() {
        let number = 10
        return function () {
            number++
            console.log(number);
        }
    }
    var fu1 = fu()
    fu1()
    fu1()

    /*
    闭包的作用
        1.防抖
            在一定时间间隔内,执行一个内容的时候 只执行最后一次
        2.节流
            在规定的时间内执行的次数,只执行第一次(总共会执行多次)
        3.函数柯里化
            将多个参数的方法 拆分为多个一个参数的方法
    */

    //防抖
    function Shake() {
        var timer = null
        return function () {
            clearTimeout(timer)//清楚上一次
            timer = setTimeout(() => {
                console.log("关闭");
            }, 3000)
        }
    }
    var shake = Shake()
    document.querySelector('.btn01').onclick = function () {
        shake()
    }

    //节流
    function Throttle() {
        var timer = null //阀门
        return function () {
            //先判断节流阀 是否开启 如果开启直接出去
            if (timer) return;
            //如果节流阀为null 操作
            timer = setTimeout(() => {
                console.log("傻狗 ! ! ! 去哪");
                //执行完毕 将节流阀设置为null
                timer = null
            }, 3000)
        }
    }
    var throttle = Throttle()
    document.querySelector(".btn02").onclick = function () {
        throttle()
    }

    //函数柯里化
    function fn(a) {
        return function (b) {
            return a + b
        }
    }
    //调用
    console.log(fn(10)(20));

    //高阶函数柯里化
    function sum(a, b, c) {
        return a + b + c
    }
    function currying(fun) {
        let arg = Array.prototype.slice.call(arguments, 1)//切割
        //统计参数 每次取一个
        return function () {
            //将原本有的参数和我这个参数做拼接
            var newArgs = arg.concat(Array.prototype.slice.call(arguments))
            //参数个数没到返回的是一个函数
            if (newArgs.length < fun.length) {//传入的参数 不等于对应的函数里面需要参数 这个时候返回一个对应的函数
                return currying.call(this, fun, ...newArgs)
            } else {
                //参数个数到了返回对应的结果
                return fun.apply(this, newArgs)
            }
        }
    }
    var fn2 = currying(sum)
    console.log(fn2(1)(2, 3));
    console.log(fn2()()(1)(2)()(3));
    console.log(fn2()()(1)()(2)()(3));
    console.log(fn2()()()()()()()()()(1, 2, 3));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值