var b = newPromise(resolve =>window.resolve = resolve) var a = newPromise(resolve => resolve(b)) a.then(() =>console.log('a')) b.then(() =>console.log('b')) resolve()
某学堂的面试题,好基友当时估计没睡醒,没答上来就直接跪了,有点狠
Generator next传参
1 2 3 4 5 6 7 8 9 10 11 12 13
function *foo(x) { let y = 2 * (yield (x + 1)) let z = yield (y / 3) return (x + y + z) } let it = foo(5) console.log(it.next(11)) console.log(it.next(12)) console.log(it.next(13))
var test = { name: 'cpp', fn: function() { console.trace() console.log(this.name) } } var name = 'wmh' test.fn() // cpp test.fn.call(this) // wmh
Three
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var fullName = '1' var obj = { fullName: '2', prop: { fullName: '3', getFullName: () => { console.log(this) returnthis.fullName } } } console.log(obj.prop.getFullName()) var test = obj.prop.getFullName console.log(test())
作用域相关
one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var a = 20 var test = { a: 40, init: function() { console.log(this.a) functiongo() { console.log(this, 'new') console.log(this.a) } go.prototype.a = 50 return go } } var p = test.init() // p() // new p() //
init改成箭头函数呢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var a = 20 var test = { a: 40, init: () => { console.log(this.a) functiongo() { console.log(this, 'new') console.log(this.a) } go.prototype.a = 50 return go } } var p = test.init() // p() // new p() //
原型链
one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
functionA() { this.a = 1; } A.prototype.b = 1;
const a = new A(); console.log(a.a); // 1 console.log(a.b); // 1
const b = new A(); b.__proto__.a = 2; A.prototype.b = 2;