先看一段非严格模式下的函数,猜猜这个里面的this是指向什么?

        function show(){            console.log(this)        }        show()

很显然,这个指向的是window

严格模式下运行class_开发语言

那么,严格模式下一模一样的代码,得到的this又是什么呢?结果就是undefined

"use strict";  function show(){      console.log(this)   }  show()

严格模式下运行class_严格模式_02

以上是我们的函数,在构造函数下又是啥情况呢?

非严格模式下,this还是指向的是window

  function User() {}        User.prototype.show=function () {            function test() {                console.log(this) // 打印出来的是window            }            test();        }        let u = new User();        u.show()

严格模式下,this指向的是undefined

 "use strict";        function User() {}        User.prototype.show=function () {            function test() {                console.log(this)            }            test();        }        let u = new User();        u.show()

同样的场景在class下面又是啥样子的呢?猜到没?它其实就是默认的严格模式啦,打印出来的是undefined~~~

 class Hd{            show(){                function test() {                    console.log(this); //  undefined                 }                 test();            }        }        let hd = new Hd();        hd.show();

所以啊,我们在声明一个类的时候,最好就使用class的方式,它相比使用函数的声明更加的先进,让我们默认在严格模式下