javascript実行環境(実行期間文脈)詳細

4802 ワード

javascript    (      )
 js   (control)        ,            ,                ,              ,               ,             。
            :

          
                                 
                                           。
this               this        。

       

       ,  eval  ,       ,       。
         ,          ,                :
1,    
ECMA      C      :
(1)            
(2)            
(3) this         

2,      
         F,      thisArg,      argumentsList ,      
(1)  this:
if(         ){
    this=thisArg;
}else if(thisArg===null||thisArg=undefined){
    this=    
}else if(Type(thisArg!==Object)){
    this=ToObject(thisArg);
}else{
    this=thisArg;
}
(2) F     [[Scope]]                ,    localEnv,
       localEnv,       localEnv
(3) code F     [[Code]]

              ,       ,      :
1,env=           
2,if(eval  ){
    configurableBindings=true;
}else{
    configurableBindings=false;
}
3,if(strict mode code){
    strict=true;
}else{
    strict=false;
}
4,if(    ){
    func=  code     [[Call]]   
    names=func     [[FormalParameters]]    
    argCount=args   
    n=0
    foreach(argName in names){
        n++
        v=n>argsCount?undefined:v=args[n]
        argAlreadyDeclared=env.HasBinding(argName)
        if(argAlreadyDeclared===false){
            env.CreateMutableBinding(argName)//          
        }
        env.SetMutableBinding(argName,v,strict)//          
    }
}
5,  code         f,       :
    fn=f Identifier
    fo=f      
    funcAlreadyDeclared=env.HasBinding(fn)
    if(funcAlreadyDeclared===false){
        env.CreateMutableBinding(fn,configurableBindings)
    }else if(env===        ){
        go=    
        existingProp= fn     go     [[GetProperty]]   
        if(existingProp [[Configurable]]    ===true){
            go.[[DefineOwnProperty]](fn,Property Descriptor {[[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true , [[Configurable]]: configurableBindings },true)
        }else if(existingProp            existingProp      {[[Writable]]: true, [[Enumerable]]: true}){
            Throw a TypeError exception
        }
        env.SetMutableBinding(fn,fo,strict)
    }
6,argumentsAlreadyDeclared=env.HasBinding(arguments)
7,if(code is function code and argumentsAlreadyDeclared===false){
    argsObj=CreateArgumentsObject(func, names, args, env , strict)
    if(strict===true){
        env.CreateImmutableBinding(arguments)
        env.InitializeImmutableBinding(arguments,argsObj)//     arguments   
    }else{
        env.CreateMutableBinding(arguments)
        env.SetMutableBinding(arguments,args,false)//     arguments  
    }
}
8,  code         d:
    dn=d Identifier
    varAlreadyDeclared=env.HasBinding(dn)
    if(varAlreadyDeclared===false){
        env.CreateMutableBinding(dn,configurableBindings)
        env.SetMutableBinding(dn,undefined,strict)
    }