---
url: https://sugarat.top/offer/review/01-20.md
---

# 01-20

## 模拟实现

1. 根据async/await与generator的关系模拟实现

```js
// gen 为generator
function myAsync(gen){

}


// 测试代码
myAsync(function* () {
    const a = yield Promise.resolve(1)
    const b = yield new Promise((res, rej) => {
        setTimeout(() => {
            res(2)
        }, 2000)
    })
    const c = yield Promise.resolve(3)
    console.log(a, b, c);

    try {
        const d = yield Promise.reject(4)
    } catch (error) {
        console.log(error);
    }

    return [a, b, c]
}).then(console.log)
// 输出
// 1 2 3
// 4
// [1,2,3]
```

## 理论

1. 考察隐式类型转换,下面if为真的有哪些

```js
if([])
if({})
if([]==false)
if({}==false)
```

2. this指向考察

```js
function a(){
    this.b = 3
}
a()
console.log(b) // ?
var b = 5
console.log(b) // ?
var c = new a()
console.log(b) // ?
a.prototype.b = 4
a.prototype.c = 5
console.log(c.b) // ?
console.log(c.c) // ?
console.log(b) // ?
```

3. 什么是动态作用域?什么是静态作用域?
4. js是动态还是静态作用域
5. 作用域考察
   1. 例1
   ```js
   var scope = "global scope";
    function checkscope() {
        var scope = "local scope";
        function f() {
            return scope;
        }
        return f();
    }
    console.log(checkscope()); // ?
   ```
   2. 例2
   ```js
    var scope = "global scope";
    function checkscope(){
        var scope = "local scope";
        function f(){
            return scope;
        }
        return f;
    }
    checkscope()(); // ?
   ```
   3. 例3
   ```js
   for(var i = 0;i<2;i++){
       setTimeout(()=>{
           for(var j = 0;j<3;j++){
               setTimeout(()=>{
                   console.log(i*j)
               },0)
           }        
       },0)
   }
   // 输出结果是多少？为什么
   // var 变为 let 结果又是多少？为什么
   ```

## dom相关

1. 如何获取一个dom对象
2. 如何获取指定dom的指定属性
3. 如何获取指定dom的指定样式
4. 如何获取指定dom的生效样式

## 朋友们的回答

* [tdk](https://www.cnblogs.com/dark-duck/p/14321961.html)
* [tl](https://juejin.cn/post/6921252628097679368/)
