const people = name hello consolelogthisname hey = consolelogname22 peoplehello peoplehey为什么请解释
people.hello()会打印出空字符串,因为this关键字在hello方法中指向了people对象,所以this.name就是people对象的name属性,而初始时name属性为空字符串。
people.hey()会报错,因为箭头函数中的this关键字不会绑定到任何对象,它的上下文是定义时的上下文。在这种情况下,箭头函数hey()是在全局作用域中定义的,所以它的上下文是全局对象。因此,console.log(name,22)中的name是在全局作用域中寻找name变量,但是该变量未定义,所以会报错。
原文地址: https://www.cveoy.top/t/topic/iHFn 著作权归作者所有。请勿转载和采集!