js 函数定义的方式 改变this指向方法 call apply bind 严格模式下 ‘use strict‘

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>点击</button>
    <script>
        // 函数定义的方式
        // 1. 自定义函数
        // function fn() {}
        // 2. 构造函数
        // var f = new Function(参数,参数3,函数题)
        // 3. 函数表达式
        // var fun = function()


        //1.普通函数 this->window
        function fun() {

        }
        fun()
            //2.对像方法 this->对像
        var b = {
            s: function() {

            }
        }
        b.s()
            //3.构造函数 this->实例对象 构造函数原型对象的this也是实例对象
        function S() {

        }
        new S()
            //4.绑定事件函数    构造函数 this->实例对象
            // 触发事件调用
            //5. 定时器函数    this->window
            // 6.立即执行函数,自动调用  this->window
            (function() {

            })()
            //改变this指向方法 call apply bind
            // call(对象,参数1,参数2....) 函数.call(对像) 主要作用实现属性继承 
            // apply(对像,[]) 1.调用函数2.改变this指向 参数必须为数组 主要用作数组
            // apply() 的主要作用var max =Math.max.apply(Math, array)
            // bind:绑定函数,只会改变this指向,不会调用,只是调用函数,参数与call同,返回原函数this改变后的新韩淑 主要用作:定时器
        var array = [1, 3, 3, 100, 2, 1, 11, 1, ]
        var max = Math.max.apply(Math, array)
        console.log(max)
            //_________________________________________________________________________
            // bind绑定后, 函数不会立即执行, 举例子
        var btn = document.querySelector("button")
        btn.onclick = function() {
            this.disabled = true;
            setTimeout(function() {
                this.disabled = false
            }.bind(this), 3000)
        }

        //严格模式下 'use strict'
        // 区别:1.变量名先声明后使用2.不可以随意删除已经声明的变量3.严格模式下this指向underfunde 
        //4.以前构造函数时不加new也可以调用,当普通函数,this指向全局 严格模式下不加new,this报错 5.不允许函数名重名
        // __________________________________________________________________________________________________
        // 闭包:指有权去访问别的函数作用域中变量的函数.被访问的变量的所在函数就是闭包函数,
        // 也就是一个作用域可以访问另外一个函数的局部变量
        function fn() {
            var num = 10;

            function fun() {
                console.log(num)
            }
        }
        // 如何实现fn外访问num?
        function fn() {
            var num = 10;

            function fun() {
                console.log(num)
            }
            return fun
        }
        var f = fn()
        f()
            // 类似f= function fun() {
            //         console.log(num)
            //     }
    </script>
</body>

</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优价实习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值