this指代计算伪代码

66 阅读1分钟
FUNCTION.this = {
    ref =  MemberExpression 的结果;                     //'()'左边的表达式的结果 
    IF Type(ref) == Reference THEN                     //如果左边的表达式的结果是引用
        IF IsPropertyReference(ref) THEN               //判定引用的基值是不是一个对象
            thisArg = ref.base                         //如果是,thisArg是这个引用的基值
        ELSE                
            thisArg = ref.base.ImplicitThisValue() //如果引用的基值不是对象,说明是个环境记录,
                                    //thisArg是它的ImplicitThisValue,绝大部分情况下是Undefined
    ELSE
        thisArg = undefined;        //如果左边的表达式的结果不是引用,thisArg是undefined
    
   IF thisArg == undefined                          
        IF 'using strict' THEN            //thisArg如果是undefined
            return   undefined            //在严格模式下,函数的this就是undefined
        ELSE
            return   全局对象              //在非严格模式下,函数的this就是window
    ELSE
        return  thisArg                   //thisArg不是undefined,函数的this就是这个基值对象
}