美文网首页
ES6-函数箭头

ES6-函数箭头

作者: 小蜜蜂_1d30 | 来源:发表于2022-08-25 18:24 被阅读0次

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

<head>
<meta charset="UTF-8" />
<script>
var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') ||
CSS.supports('top: constant(a)'))
document.write(
'<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' +
(coverSupport ? ', viewport-fit=cover' : '') + '" />')
</script>
<title></title>
</head>

<body>
<div id="ad" style="width:500px;height:500px;border:1px solid #000">

</div>
<script type="module" src="/main.js"></script>
<script>
//1.this 是静态的,this始终指向函数声明时所在作用域下的this的值
function getName() {
console.log(this.name)
}
let getName2 = () => {
console.log(this.name)
}
//设置window 对象的name属性
window.name = '尚硅谷';
const school = {
name: 'ATGUIGU'
}
//直接调用
getName();//尚硅谷
getName2();//尚硅谷
//call 方法调用
getName.call(school);//ATGUIGU
getName2.call(school);//尚硅谷

    //2.不能作为构造函数实例化对象
    let Person = (name, age) => {
        this.name = name;
        this.age = age;
    }
    let me = new Person('xiao', 20);
    console.log(me)//Uncaught SyntaxError: Identifier 'Person' has already been declared
    at < anonymous >: 1: 1
    //3.不能使用arguments变量
    let fn = () => {
        console.log(arguments)
    }
    fn(1, 2, 3)// Uncaught ReferenceError: arguments is not defined
    //4.箭头函数的简写
    //(1)省略小括号,当形参有且只有一个的时候
    let add = n => {
        return n + n;
    }
    console.log(add(9));//18
    //(2)省略花括号,当代码体只有一条语句的时候,此时return 必须省略,而且语句的执行结果就是函数的返回值
    let pow = (n) => n * n;
    console.log(pow(9))//81
    //需求-1 点击div 2S后颜色变成[粉色]
    //获取元素
    let ad = document.getElementById('ad');
    //获取元素
    ad.addEventListener('click', function () {
        //保存this的值
        // let that = this;
        //定时器
        // setTimeout(function () {
        //     //修改背景颜色this
        //     //console.log(this);
        //     that.style.background = 'pink'
        // }, 2000)
        setTimeout(() => {
            //修改背景颜色this
            //console.log(this);
            this.style.background = 'pink'
        }, 2000)
    })
    //需求-2 从数组中返回偶数的元素
    const arr = [1, 3, 6, 10, 100, 25];
    const result = arr.filter(fucntion(item)=> {
        if (item % 2 === 0) {
            return true
        } else {
            return false
        }
    })

    const result = arr.filter(item => item % 2 === 0)
    console.log(result)//[6,100]

    //箭头函数适合与this无关的回调,定时器,数组的方法回调
    //箭头函数不适合与this有关的回调,事件回调,对象的方法
    {
        name: '尚硅谷',
            getName: () => {
                this.name
            }
    }
</script>

</body>

</html>

相关文章

  • ES6-箭头函数

    箭头函数中的this ES6函数参数默认值 箭头函数不适用的场景

  • es6-箭头函数

    1 变量定义 let只在局部代码块中有效 const不希望修改,差不多跟常量一个意思吧 2 箭头函数 【主...

  • ES6-箭头函数

    基本语法 当有多个参数时 (参数1,参数2,...参数n)=>{函数体} 当只有一个return时,{}可以省略 ...

  • ES6-箭头函数

    ES6允许使用箭头(=>)定义函数,箭头函数的语法多变,根据实际的使用场景有多种形式,但需要由函数参数、箭头和函数...

  • ES6-函数箭头

    var coverSupport = 'CSS' in window && typ...

  • ES6-箭头函数

    声明和特点 声明 特点 1.this是静态的,this始终指向声明时所在作用域下的值 2.不能作为构造函数实例化对...

  • ES6-函数与箭头函数

    一、设置默认值 二、箭头函数 单个参数 多个参数 实际应用 箭头函数体内的this指向定义时所在的对象,而非实例化...

  • ES6~箭头函数

    什么是箭头函数 单表达式箭头函数 相当于 多表达式箭头函数 箭头函数相当于匿名函数,并且简化了函数定义。箭头函数有...

  • 箭头函数和立即执行函数

    箭头函数 箭头函数和普通函数有什么区别?如果把箭头函数转换为不用箭头函数的形式,如何转换主要是this的差别,箭头...

  • 学习 ES 6 箭头函数

    箭头函数的用法 ES6 允许使用“箭头”(=>)定义函数。 箭头函数的一个用处是简化回调函数。 箭头函数 this...

网友评论

      本文标题:ES6-函数箭头

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