how2j.cn

示例 1 : 参数的作用域   
示例 2 : 全局变量的作用域   

示例 1 :

参数的作用域

一个参数的作用域就在这个函数内部,超出函数就看不见该参数了
"); window.frames["iframe_show1096"].document.write(decodeHtml($("textarea#stepcodeTextarea1096").val())); window.frames["iframe_show1096"].document.close(); $(window.frames["iframe_show1096"]).load(function(){ $("#iframe_show1096").height($("#iframe_show1096").contents().find("body").height()+showittryitheight); }); $("#iframe_show1096").height($("#iframe_show1096").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function f1(a){ document.write('参数的作用域在函数以内,其值是 '+a);//参数a的作用范围,所以打印出来是5; } function f2(){ document.write('在函数里不能访问其他函数的参数'+a); //不在参数a的作用范围,是一个未声明的变量,无法打印 } f1(5); f2(); document.write('在函数外也不能访问'+a);//也不在参数a的作用范围,是一个未声明的变量,无法打印 </script>
<script>

function f1(a){
   document.write('参数的作用域在函数以内,其值是 '+a);//参数a的作用范围,所以打印出来是5;
}

function f2(){
   document.write('在函数里不能访问其他函数的参数'+a); //不在参数a的作用范围,是一个未声明的变量,无法打印
}

f1(5);
f2();
document.write('在函数外也不能访问'+a);//也不在参数a的作用范围,是一个未声明的变量,无法打印

</script>
"); window.frames["iframe1096"].document.write(decodeHtml(code1096)); window.frames["iframe1096"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1096"]).load(function(){ $("#iframe1096").height($("#iframe1096").contents().find("body").height()+showittryitheight); }); $("#iframe1096").height($("#iframe1096").contents().find("body").height()+showittryitheight); alreadyWriteCode1096 = code1096; $("#rendering1096").hide(); $("#rendered1096").show(); } var tRereshRetry2DemoPanel1096 = setInterval(rereshRetry2DemoPanel1096,1000); var binded1096 = false; $("textarea#stepcodeTextarea1096").keyup(function(){ if(!binded1096){ $(window).bind('beforeunload',function(){ binded1096 = true; return "xxxx"; }); } var newCode = $(this).val() code1096 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1096!=newCode){ // window.frames["iframe1096"].document.write("
"); // window.frames["iframe1096"].document.write(decodeHtml($("textarea#stepcodeTextarea1096").val())); // window.frames["iframe1096"].document.close(); // $(window.frames["iframe1096"]).load(function(){ // $("#iframe1096").height($("#iframe1096").contents().find("body").height()+showittryitheight); // }); // code1096 = newCode; // } }); $(".tryButton1096").click(function(){ $("#tryDiv1096").show(); $("#stepcodeTextarea1096").focus(); $("#stepcodeTextarea1096").height(200); $("#iframe1096").height(0); window.frames["iframe1096"].document.write("
"); window.frames["iframe1096"].document.write(decodeHtml($("textarea#stepcodeTextarea1096").val())); window.frames["iframe1096"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1096"]).load(function(){ $("#iframe1096").height($("#iframe1096").contents().find("body").height()+showittryitheight); }); $("#iframe1096").height($("#iframe1096").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1096.focus(); editor1096.setSize(null, "250"); $("#rendering1096").hide(); $("#rendered1096").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1096 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1096"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1096.on("change",function(doc){ if(!binded1096){ $(window).bind('beforeunload',function(){ binded1096 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1096 = newCode; $("textarea#stepcodeTextarea1096").val(newCode); if(alreadyWriteCode1096!=code1096){ lastModifedTime1096 = new Date().getTime(); $("#rendering1096").show(); $("#rendered1096").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1096 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1096.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1096").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

全局变量的作用域

"); window.frames["iframe_show1097"].document.write(decodeHtml($("textarea#stepcodeTextarea1097").val())); window.frames["iframe_show1097"].document.close(); $(window.frames["iframe_show1097"]).load(function(){ $("#iframe_show1097").height($("#iframe_show1097").contents().find("body").height()+showittryitheight); }); $("#iframe_show1097").height($("#iframe_show1097").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var a = 0; //定义在函数前面,即全局变量,所有函数都可以访问 function f1(){ document.write('通过函数f1 设置全局变量a的值 为 5'); a = 5; //能够访问 } function f2(){ document.write('通过函数f2 访问并打印全局变量a的值 '+a); //能够访问 } f1(); //通过f1,设置a的值 document.write('<br>'); f2(); //通过f2, 打印a的值 </script>
<script>
var a = 0; //定义在函数前面,即全局变量,所有函数都可以访问

function f1(){
  document.write('通过函数f1 设置全局变量a的值 为 5');
  a = 5; //能够访问
}

function f2(){
  document.write('通过函数f2 访问并打印全局变量a的值 '+a); //能够访问
}

f1(); //通过f1,设置a的值
document.write('<br>');
f2(); //通过f2,  打印a的值
</script>
"); window.frames["iframe1097"].document.write(decodeHtml(code1097)); window.frames["iframe1097"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1097"]).load(function(){ $("#iframe1097").height($("#iframe1097").contents().find("body").height()+showittryitheight); }); $("#iframe1097").height($("#iframe1097").contents().find("body").height()+showittryitheight); alreadyWriteCode1097 = code1097; $("#rendering1097").hide(); $("#rendered1097").show(); } var tRereshRetry2DemoPanel1097 = setInterval(rereshRetry2DemoPanel1097,1000); var binded1097 = false; $("textarea#stepcodeTextarea1097").keyup(function(){ if(!binded1097){ $(window).bind('beforeunload',function(){ binded1097 = true; return "xxxx"; }); } var newCode = $(this).val() code1097 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1097!=newCode){ // window.frames["iframe1097"].document.write("
"); // window.frames["iframe1097"].document.write(decodeHtml($("textarea#stepcodeTextarea1097").val())); // window.frames["iframe1097"].document.close(); // $(window.frames["iframe1097"]).load(function(){ // $("#iframe1097").height($("#iframe1097").contents().find("body").height()+showittryitheight); // }); // code1097 = newCode; // } }); $(".tryButton1097").click(function(){ $("#tryDiv1097").show(); $("#stepcodeTextarea1097").focus(); $("#stepcodeTextarea1097").height(200); $("#iframe1097").height(0); window.frames["iframe1097"].document.write("
"); window.frames["iframe1097"].document.write(decodeHtml($("textarea#stepcodeTextarea1097").val())); window.frames["iframe1097"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1097"]).load(function(){ $("#iframe1097").height($("#iframe1097").contents().find("body").height()+showittryitheight); }); $("#iframe1097").height($("#iframe1097").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1097.focus(); editor1097.setSize(null, "250"); $("#rendering1097").hide(); $("#rendered1097").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1097 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1097"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1097.on("change",function(doc){ if(!binded1097){ $(window).bind('beforeunload',function(){ binded1097 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1097 = newCode; $("textarea#stepcodeTextarea1097").val(newCode); if(alreadyWriteCode1097!=code1097){ lastModifedTime1097 = new Date().getTime(); $("#rendering1097").show(); $("#rendered1097").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1097 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1097.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1097").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JavaScript-作用域 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图