资源描述:
《js全局变量与局部变量预解析与作用域链详解》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、js全局变量与局部变量预解析与作用域链详解EachJSexecutionenvironmenthasascopechainassociatedwithit,whichisanobjectlistorobjectchainQuerythex:variablenameresolution(variable,name,resolution)process,whichbeginstovieweachobjectofthescopechain,Ifso,returnvalues,ifnot,continuetoquerythenextobje
2、ct,andsoonThepriorityofthescopechain:thecallobjectofthenestedfunction〉callobject>globalobjectAccordingtotheaboveunderstanding,theprocessofJSinitializationisdescribed:BeforetheJSinterpreterexecutesanycode,theglobalobjectiscreatedInitializesattributesinglobalobjectswithp
3、redefinedvaluesandfunctions,eg.Math,Infinity,andparselntTheVaRdeclarationoutsidethesearchfunctioncreatesthepropertycorrespondingtotheglobalobjectandisinitializedtoundefinedCreatesaglobalexecutionenvironmentwithonlyoneobjectinthescopechain-globalobjectsExecutecodeinsequ
4、enceWhenyouencounteraVARdeclaration,theassignmentstatementgivesthepropertycorrespondingtotheglobalobjectWhenanundeclaredassignmentstatementisencountered,addthecorrespondingpropertyintheglobalobject,andassignthevalueWhenafunctioncalliscreated,thecallingobjectiscreatedTh
5、eVaRdeclarationandargumentsinthesearchfunctioncreatethepropertycorrespondingtothecallingobject,initializedtoundefinedCreateafunctionexecutionenvironment,scopechain-firstobject:callobject;secondobject:globalobjectExecutecodeinsequenceWhenyouencounteraVARdeclarationassig
6、nmentstatement,assignthecorrespondingattributetothecallingobjectWhenanundeclaredassignmentstatementisencountered,addthecorrespondingpropertyintheglobalobject,andassignthevalueWhenafunctioncalliscreated,thecallingobjectofthenestedfunctioniscreatedSearchforVARdeclaration
7、sandargumentsinnestedfunctions,createthecorrespondingattributesofthecallingobjectofthenestedfunction,andinitializeittoimdefinedCreatenestedfunctionexecutionenvironment,scopechain-firstobject:callobjectofnestedfunction;secondobject:callobject;thirdobject:globalobjectAnd
8、soonEgl.Varscope二"global";Function,f(){Alert(scope);Varscope二"local"";Alert(scope);(f);Process:Createsaglobalobject,s