js的this奇怪现象,类undefined字面量window

类新建的对象,执行是undefined

class Animal {
  say() {
    console.log("say", this);
  }
}
let obj = new Animal();
let say = obj.say;
say();

字面量新建的对象,执行是window

let obj = {
  say() {
    console.log("say", this);
  },
};
let say = obj.say;
say();
posted @ 2021-12-08 18:03  jerry-mengjie  阅读(120)  评论(0)    收藏  举报