<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>类</title>
<script>
class MyClass{

constructor() {
// this.fn = this.fn.bind(this); 将fn方法的this绑定为当前实例
}
/*
* 类中的所有代码都会在严格模式下执行
* 严格模式下其中一个特点就是,函数的this不在是window,而是undefined
*
* 注意:
* 在类中方法的this不是固定的
* 以方法形式调用时,this就是当前的实例
* 以函数形式调用,this是undefined
* 在开发时,在有些场景下,我们希望方法中的this是固定的,不会因调用方式不同而改变
* 如果遇到上述需求,可以使用箭头函数来定义类中的方法
* 如果类中的方法是以箭头函数定义的,则方法中的this恒为当前实例,不会改变
*
* */

// fn(){
// console.log('-->',this);
// }

fn = () => {
console.log('-->',this);
};

}

const mc = new MyClass();


const test = mc.fn;

mc.fn(); // mc
test(); // undefined

const fn2 = function (){
console.log(this);
};

// fn2();


</script>
</head>
<body>

</body>
</html>