<!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>
网友评论