Document

this 指向

    谁调用指向谁,默认为 window/global/undefined('use strict')
  • 不适用于箭头函数
  • this 优先级
  • new > apply,call,bing, > 对象调用
  • 在海量的学习资料中,甄别出对自己有用的东西
  • 制定合适的学习计划,不要一下子学很多
  • 把知识形成经验,学了有什么用?要基于业务抽象模型,思考业务的抽象,基于知识点,套用模型,学了东西想想自己写过的东西哪些地方可以用。 用自己的技术降本增效。学东西不是为了感动自己,是做对企业有用的东西。有更多的机会,从产品的角度思考问题,性能,交互,细节。
  • 洞悉企业,领导,市场导向,国家导向,能否让技术快速变现
  • 知识体系
                <ul>
                    <li>学完就忘</li>
                    <li>xx知识不知道自己不知道</li>
                </ul>
            </li>
        </ul>
    </div>
    <div>
        <h6>程序是什么,是怎么运行的</h6>
        <ul>
            <li>
                <a href="https://astexplorer.net/" target="_blank">
                    astexplorer
                </a>
            </li>
            <li>
                <code>
                    console.log('hello');
                </code>
                <ul>
                    <li>预编译阶段</li>
                    <li>runtime阶段</li>
                </ul>
                <pre>
                写代码其实就是写字符串,预编译的时候先进行分词,
                console   /    log   .....
                词法分析.   ->  ast
    
                parse -> transform -> generator
                js在预编译阶段会有,变量声明,变量提升,所有非表达式的函数声明进行变量提升
                作用域是 根据名称去查找变量的规则
                词法作用域/静态作用域
                定义在词法阶段的作用域
                动态作用域/上下文
                运行时的作用域
    
                什么时候上下文和作用域不一致呢
    
                闭包
                </pre>
            </li>
            <li>
                bind的作用:修改this指向,给方法传参,并返回一个新的方法,注意考虑new 的优先级比bind高
                    <pre>
                        function myBind(context, ...args) {
                            if (typeof this !== 'function') {
                              throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
                            }
                          
                            const bound = function (...innerArgs) {
                              return this instanceof bound
                                ? new (this.constructor)(...args, ...innerArgs)
                                : this.apply(context, args.concat(innerArgs));
                            };
                          
                            bound.prototype = Object.create(this.prototype);
                          
                            return bound;
                          }
                    </code>
                    <pre>
                        Function.prototype.myCall = function(context, ...args) {
                            if (typeof this !== 'function') {
                              throw new TypeError('Function.prototype.call - what is trying to be called is not callable');
                            }
                          
                            context = context || globalThis;
                          
                            context.fn = this;
                          
                            const result = context.fn(...args);
                          
                            delete context.fn;
                          
                            return result;
                          };
                    </code>
                    <pre>
                        Function.prototype.myApply = function(context, args) {
                            if (typeof this !== 'function') {
                              throw new TypeError('Function.prototype.apply - what is trying to be called is not callable');
                            }
                          
                            context = context || globalThis;
                          
                            context.fn = this;
                          
                            const result = context.fn(...args);
                          
                            delete context.fn;
                          
                            return result;
                          };
                    </code>
            </li>
            <li>
                todo.   手写 bind, apply, call
            </li>
        </ul>
    </div>