输出题

1. window、obj、person1、person2

var name = 'window'

function Person(name) {
  this.name = name
  this.obj = {
    name: 'obj',
    foo1: function () {
      return function () {
        console.log(this.name)
      }
    },
    foo2: function () {
      return () => {
        console.log(this.name)
      }
    }
  }
}

var person1 = new Person('person1')
var person2 = new Person('person2')

person1.obj.foo1()()   // window
person1.obj.foo1.call(person2)() // window
person1.obj.foo1().call(person2) // person2

person1.obj.foo2()() // obj
person1.obj.foo2.call(person2)() // person2
person1.obj.foo2().call(person2) // obj

2. promise

const promise = new Promise((resolve, reject) => {
    console.log(1);
    resolve();
    console.log(2);
    throw new Error('error')
    reject('error');
})
promise.then(() => {
    console.log(3);
}).catch(e => console.log(e))


console.log(4);

// 1243

3.

var id = 'GLOBAL';
var obj = {
  id: 'OBJ',
  a: function(){
    console.log(this.id);
  },
  b: () => {
    console.log(this.id);
  }
};
obj.a(); // OBJ
obj.b(); // GLOBAL
new obj.a(); // undefined
new obj.b(); // error
Last Updated:
Contributors: Rain9