1 / 16
文档名称:

js变量作用域.doc

格式:doc   大小:96KB   页数:16页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

js变量作用域.doc

上传人:ttteee8 2019/9/1 文件大小:96 KB

下载得到文件列表

js变量作用域.doc

相关文档

文档介绍

文档介绍::..js变量作用域Eachlanguagehastheconceptofvariable,,thefollowingfunction:lfunction,Student(name,age,from)2{=name:4this・age=age;5this・from=from;=function(){78return,z,my,information,is,name:z,,〃++〃,〃age〃,〃++〃, +this・from;9}10}TheStudentclasshasthreevariables,name(name),age(age),andfrom(nativeplace)・Thesethreevariablesmakeuptheinformationdescribinganobject・Ofcourse,thereisalsoawaytoreturninformationfromStudent・essedandusedanywhereuntilitisdestroyed?Thinkcarefully,theaboverequirementisexcessive,becausesomeofthevariablesinafunctionisnolongerused,butifthevariablesstillexist,takeupsystemresources,theysaid:"stoodMaokengnotpull#$%〃・Sowehaveadiscussiononthetimelyandon-demanddestructionofvariables・Well,let'sgetdowntothepoint・AsfarasI'mconcerned,JSsupportsseveraltypesofvariables:localvariables,classvariables,privatevariables,instancevariables,staticvariables,andglobalvariables・Next,:Localvariablesgenerallyreferstotheeffectivevariablesintherangeofvariablesis{},effectivestatementsinsidetheblock,suchas:lfunctionfoo(flag)2{3varsum二0;4if(flag二true)6varindex;7for(index=0;index<10;index++)(89sum+二index;10}11}("indexis":〃+index+〃,/<br>,/);13returnsum;14}15//("sumis":〃+sum+〃〃〈br〉〃);("resultis〃:〃+foo〃(true)+,z<br>z,);Whenthecodeisexecuted,theoutputresultsare:,zindexis:undefined〃and"resultis:0〃・Wecanseethatthevalueoftheindexvariablethatwewanttooutputisundefined,thatis,undefined・Sowecanseethattheindexvariableisdestroyedaftertheendoftheifstatementblock・Whataboutthe"sunTvariables?Thisvariableisdestroyedaftertheexecutionofthefoo()functionsection・IfyouremovethestatementIarmotated,andthenexecute,youwillfindthatthesystemwillbewrong・ItisworthnotingthatifIchangetheabovefoo()functiontothefollowing:lfunctionfoo(flag)2{3varsum二0;4for(VaRindex=0;index<10;index++;){56sum+二index;7}