一. this指向

1.

function foo() {
    console.log( this.a );
}
function doFoo() {
    foo();
}
var obj = {
    a: 1,
    doFoo: doFoo
};
var a = 2; 
obj.doFoo()   // 2

2.

var inner = 'window'
function say() {
    console.log(inner)
    console.log(this.inner)
}
let obj = (function f() {
    var inner = 'inner-1'
    return {
        inner: 'inner-2',
        say:  function(){
            console.log(inner);
            console.log(this.inner);
        }
    }
})()
say()  // window, window
obj.say()      // inner-1 inner-2
obj.say = say
obj.say()    // window inner-2

二.事件循环

1.

// 虾皮
setTimeout(() => {
  console.log('1');
  Promise.resolve().then(() => {
    console.log('2');
  });
}, 0);
new Promise((resolve) => {
  console.log('3');
  resolve();
}).then(() => {
  console.log('4');
  setTimeout(() => {
    console.log('5');
  }, 0);
}).then(() => {
  console.log('6');
});
console.log('7');

// 3 7 4 6 1 2 5

2.

     async function async1() {
        console.log('async1 start');
        await async2();
        console.log('async1 end');
      }
      async function async2() {
        console.log('async2');
      }
      console.log('script start');
      setTimeout(function() {
        console.log('setTimeout');
      }, 0);
      async1();
      new Promise(function(resolve) {
        console.log('promise1');
        resolve();
      }).then(function() {
        console.log('promise2');
      });
      console.log('script end');

      // script start, async1 start, async2, promise1, script end,  async1 end, promise2, setTimeout

3.

Promise.resolve().then(() => {
    console.log('promise1');
    const timer2 = setTimeout(() => {
        console.log('timer2')
    }, 0)
});
const timer1 = setTimeout(() => {
    console.log('timer1')
    Promise.resolve().then(() => {
        console.log('promise2')
    })
}, 0)
console.log('start');


// start  promise1  timer1 promise2  timer2

4.

const promise = new Promise((reslove, reject) => {
    console.log('a');
    reslove('b')
    console.log('c')
    reject('d')
    setTimeout(() => console.log('e'),0)
})
promise.then(data => {
    console.log(data);
}).catch(err => console.log(err))
console.log('f');

// a c f b e

new 与 继承

function T1() {
 this.name = 't1';
 this.age = 19;
}
 
function T2() {
 this.name = 't2';
 this.age = 19;
 return 19;  
}
 
function T3() {
 this.name = 't3';
 this.age = 19;
 return {
   name: 't',
//    age: 20
 };  
}
 
function T4() {
 this.name = 't4';
 this.age = 19;
}
 
console.log(new T1()); //  {name:'t1',age:19}
console.log(new T2()); //  {name:'t1',age:19}
console.log(new T3()); //  {name: "t"}
 
T4.prototype = new T1();
T4.prototype.type = 'expert';
 
const t4 = new T4();
console.log(t4); //  T4 {name: "t4", age: 19}
console.log(t4.type); // expert
console.log(t4 instanceof T1); //  true
console.log(t4 instanceof T2); // false
console.log(t4 instanceof T4); // true

三.变量提升,函数提升

4种变形

// 1.
a()
function a() {
    console.log(1);
}
a()
var a = function() {
    console.log(2);
}
a()


// 2.
a()
var a = function() {
    console.log(1);
}
a()
var a = function() {
    console.log(2);
}
a()


// 3.
a()
function a() {
    console.log(1);
}
a()
function a() {
    console.log(2);
}
a()

// 4.
a()
var a = function() {
    console.log(1);
}
a()
function a() {
    console.log(2);
}
a()

晚起的虫儿
542 声望48 粉丝

一起成长~