how2j.cn

示例 1 : 伪对象   
示例 2 : 转换为字符串   
示例 3 : 数字转字符串   
示例 4 : 转换为数字   
示例 5 : 转换为Boolean   
示例 6 : Number()和parseInt()的区别   
示例 7 : String()和toString()的区别   

示例 1 :

伪对象

伪对象概念:javascript是一门很有意思的语言,即便是基本类型,也是伪对象,所以他们都有属性和方法。
变量a的类型是字符串,通过调用其为伪对象的属性length获取其长度
"); window.frames["iframe_show1082"].document.write(decodeHtml($("textarea#stepcodeTextarea1082").val())); window.frames["iframe_show1082"].document.close(); $(window.frames["iframe_show1082"]).load(function(){ $("#iframe_show1082").height($("#iframe_show1082").contents().find("body").height()+showittryitheight); }); $("#iframe_show1082").height($("#iframe_show1082").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var a="hello javascript"; document.write("变量a的类型是:"+(typeof a)); document.write("<br>"); document.write("变量a的长度是:"+a.length); </script>
<script>
  var a="hello javascript"; 
  document.write("变量a的类型是:"+(typeof a));
  document.write("<br>");
  document.write("变量a的长度是:"+a.length);
</script>
"); window.frames["iframe1082"].document.write(decodeHtml(code1082)); window.frames["iframe1082"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1082"]).load(function(){ $("#iframe1082").height($("#iframe1082").contents().find("body").height()+showittryitheight); }); $("#iframe1082").height($("#iframe1082").contents().find("body").height()+showittryitheight); alreadyWriteCode1082 = code1082; $("#rendering1082").hide(); $("#rendered1082").show(); } var tRereshRetry2DemoPanel1082 = setInterval(rereshRetry2DemoPanel1082,1000); var binded1082 = false; $("textarea#stepcodeTextarea1082").keyup(function(){ if(!binded1082){ $(window).bind('beforeunload',function(){ binded1082 = true; return "xxxx"; }); } var newCode = $(this).val() code1082 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1082!=newCode){ // window.frames["iframe1082"].document.write("
"); // window.frames["iframe1082"].document.write(decodeHtml($("textarea#stepcodeTextarea1082").val())); // window.frames["iframe1082"].document.close(); // $(window.frames["iframe1082"]).load(function(){ // $("#iframe1082").height($("#iframe1082").contents().find("body").height()+showittryitheight); // }); // code1082 = newCode; // } }); $(".tryButton1082").click(function(){ $("#tryDiv1082").show(); $("#stepcodeTextarea1082").focus(); $("#stepcodeTextarea1082").height(200); $("#iframe1082").height(0); window.frames["iframe1082"].document.write("
"); window.frames["iframe1082"].document.write(decodeHtml($("textarea#stepcodeTextarea1082").val())); window.frames["iframe1082"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1082"]).load(function(){ $("#iframe1082").height($("#iframe1082").contents().find("body").height()+showittryitheight); }); $("#iframe1082").height($("#iframe1082").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1082.focus(); editor1082.setSize(null, "250"); $("#rendering1082").hide(); $("#rendered1082").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 editor1082 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1082"), { 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); } } }); editor1082.on("change",function(doc){ if(!binded1082){ $(window).bind('beforeunload',function(){ binded1082 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1082 = newCode; $("textarea#stepcodeTextarea1082").val(newCode); if(alreadyWriteCode1082!=code1082){ lastModifedTime1082 = new Date().getTime(); $("#rendering1082").show(); $("#rendered1082").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1082 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1082.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1082").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

转换为字符串

无论是Number,Boolean还是String都有一个toString方法,用于转换为字符串
"); window.frames["iframe_show1083"].document.write(decodeHtml($("textarea#stepcodeTextarea1083").val())); window.frames["iframe_show1083"].document.close(); $(window.frames["iframe_show1083"]).load(function(){ $("#iframe_show1083").height($("#iframe_show1083").contents().find("body").height()+showittryitheight); }); $("#iframe_show1083").height($("#iframe_show1083").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var a=10; document.write("数字 "+a+" 转换为字符串"+a.toString()); document.write("<br>"); var b=true; document.write("布尔 "+b+" 转换为字符串"+b.toString()); document.write("<br>"); var c="hello javascript"; document.write("字符串 "+c+" 转换为字符串 "+c.toString()); document.write("<br>"); </script>
<script>
  var a=10; 
  document.write("数字 "+a+" 转换为字符串"+a.toString());
  document.write("<br>");

  var b=true; 
  document.write("布尔 "+b+" 转换为字符串"+b.toString());
  document.write("<br>");

  var c="hello javascript"; 
  document.write("字符串 "+c+" 转换为字符串 "+c.toString());
  document.write("<br>");

</script>
"); window.frames["iframe1083"].document.write(decodeHtml(code1083)); window.frames["iframe1083"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1083"]).load(function(){ $("#iframe1083").height($("#iframe1083").contents().find("body").height()+showittryitheight); }); $("#iframe1083").height($("#iframe1083").contents().find("body").height()+showittryitheight); alreadyWriteCode1083 = code1083; $("#rendering1083").hide(); $("#rendered1083").show(); } var tRereshRetry2DemoPanel1083 = setInterval(rereshRetry2DemoPanel1083,1000); var binded1083 = false; $("textarea#stepcodeTextarea1083").keyup(function(){ if(!binded1083){ $(window).bind('beforeunload',function(){ binded1083 = true; return "xxxx"; }); } var newCode = $(this).val() code1083 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1083!=newCode){ // window.frames["iframe1083"].document.write("
"); // window.frames["iframe1083"].document.write(decodeHtml($("textarea#stepcodeTextarea1083").val())); // window.frames["iframe1083"].document.close(); // $(window.frames["iframe1083"]).load(function(){ // $("#iframe1083").height($("#iframe1083").contents().find("body").height()+showittryitheight); // }); // code1083 = newCode; // } }); $(".tryButton1083").click(function(){ $("#tryDiv1083").show(); $("#stepcodeTextarea1083").focus(); $("#stepcodeTextarea1083").height(200); $("#iframe1083").height(0); window.frames["iframe1083"].document.write("
"); window.frames["iframe1083"].document.write(decodeHtml($("textarea#stepcodeTextarea1083").val())); window.frames["iframe1083"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1083"]).load(function(){ $("#iframe1083").height($("#iframe1083").contents().find("body").height()+showittryitheight); }); $("#iframe1083").height($("#iframe1083").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1083.focus(); editor1083.setSize(null, "250"); $("#rendering1083").hide(); $("#rendered1083").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 editor1083 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1083"), { 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); } } }); editor1083.on("change",function(doc){ if(!binded1083){ $(window).bind('beforeunload',function(){ binded1083 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1083 = newCode; $("textarea#stepcodeTextarea1083").val(newCode); if(alreadyWriteCode1083!=code1083){ lastModifedTime1083 = new Date().getTime(); $("#rendering1083").show(); $("#rendered1083").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1083 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1083.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1083").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

数字转字符串

Number转换为字符串的时候有默认模式基模式两种
"); window.frames["iframe_show1084"].document.write(decodeHtml($("textarea#stepcodeTextarea1084").val())); window.frames["iframe_show1084"].document.close(); $(window.frames["iframe_show1084"]).load(function(){ $("#iframe_show1084").height($("#iframe_show1084").contents().find("body").height()+showittryitheight); }); $("#iframe_show1084").height($("#iframe_show1084").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var a=10; document.write('默认模式下,数字10转换为十进制的'+a.toString()); //默认模式,即十进制 document.write("<br>"); document.write('基模式下,数字10转换为二进制的'+a.toString(2)); //基模式,二进制 document.write("<br>"); document.write('基模式下,数字10转换为八进制的'+a.toString(8)); //基模式,八进制 document.write("<br>"); document.write('基模式下,数字10转换为十六进制的'+a.toString(16)); //基模式,十六进制 document.write("<br>"); </script>
<script>
  var a=10; 
  document.write('默认模式下,数字10转换为十进制的'+a.toString()); //默认模式,即十进制
  document.write("<br>"); 

  document.write('基模式下,数字10转换为二进制的'+a.toString(2)); //基模式,二进制
  document.write("<br>"); 
  
  document.write('基模式下,数字10转换为八进制的'+a.toString(8)); //基模式,八进制
  document.write("<br>"); 

  document.write('基模式下,数字10转换为十六进制的'+a.toString(16)); //基模式,十六进制
  document.write("<br>"); 

</script>
"); window.frames["iframe1084"].document.write(decodeHtml(code1084)); window.frames["iframe1084"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1084"]).load(function(){ $("#iframe1084").height($("#iframe1084").contents().find("body").height()+showittryitheight); }); $("#iframe1084").height($("#iframe1084").contents().find("body").height()+showittryitheight); alreadyWriteCode1084 = code1084; $("#rendering1084").hide(); $("#rendered1084").show(); } var tRereshRetry2DemoPanel1084 = setInterval(rereshRetry2DemoPanel1084,1000); var binded1084 = false; $("textarea#stepcodeTextarea1084").keyup(function(){ if(!binded1084){ $(window).bind('beforeunload',function(){ binded1084 = true; return "xxxx"; }); } var newCode = $(this).val() code1084 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1084!=newCode){ // window.frames["iframe1084"].document.write("
"); // window.frames["iframe1084"].document.write(decodeHtml($("textarea#stepcodeTextarea1084").val())); // window.frames["iframe1084"].document.close(); // $(window.frames["iframe1084"]).load(function(){ // $("#iframe1084").height($("#iframe1084").contents().find("body").height()+showittryitheight); // }); // code1084 = newCode; // } }); $(".tryButton1084").click(function(){ $("#tryDiv1084").show(); $("#stepcodeTextarea1084").focus(); $("#stepcodeTextarea1084").height(200); $("#iframe1084").height(0); window.frames["iframe1084"].document.write("
"); window.frames["iframe1084"].document.write(decodeHtml($("textarea#stepcodeTextarea1084").val())); window.frames["iframe1084"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1084"]).load(function(){ $("#iframe1084").height($("#iframe1084").contents().find("body").height()+showittryitheight); }); $("#iframe1084").height($("#iframe1084").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1084.focus(); editor1084.setSize(null, "250"); $("#rendering1084").hide(); $("#rendered1084").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 editor1084 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1084"), { 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); } } }); editor1084.on("change",function(doc){ if(!binded1084){ $(window).bind('beforeunload',function(){ binded1084 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1084 = newCode; $("textarea#stepcodeTextarea1084").val(newCode); if(alreadyWriteCode1084!=code1084){ lastModifedTime1084 = new Date().getTime(); $("#rendering1084").show(); $("#rendered1084").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1084 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1084.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1084").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

转换为数字

javascript分别提供内置函数 parseInt()和parseFloat(),转换为数字

注:如果被转换的字符串,同时由数字和字符构成,那么parseInt会一直定位数字,直到出现非字符。 所以"10abc" 会被转换为 10

思考题: 字符串"10abc8" 又会被转换为多少呢?
"); window.frames["iframe_show1085"].document.write(decodeHtml($("textarea#stepcodeTextarea1085").val())); window.frames["iframe_show1085"].document.close(); $(window.frames["iframe_show1085"]).load(function(){ $("#iframe_show1085").height($("#iframe_show1085").contents().find("body").height()+showittryitheight); }); $("#iframe_show1085").height($("#iframe_show1085").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> document.write("字符串的\"10\"转换为数字的:"+parseInt("10")); //转换整数 document.write("<br>"); document.write("字符串的\"3.14\"转换为数字的:"+parseFloat("3.14"));//转换浮点数 document.write("<br>"); document.write("字符串的\"10abc\"转换为数字的:"+parseInt("10abc")); //判断每一位,直到发现不是数字的那一位 document.write("<br>"); document.write("字符串的\"hello javascript\"转换为数字的:"+parseInt("hello javascript")); //如果完全不包含数字,则返回NaN - Not a Number document.write("<br>"); </script>
<script>
  document.write("字符串的\"10\"转换为数字的:"+parseInt("10")); //转换整数
  document.write("<br>");
  document.write("字符串的\"3.14\"转换为数字的:"+parseFloat("3.14"));//转换浮点数
  document.write("<br>");
  document.write("字符串的\"10abc\"转换为数字的:"+parseInt("10abc")); //判断每一位,直到发现不是数字的那一位
  document.write("<br>");

  document.write("字符串的\"hello javascript\"转换为数字的:"+parseInt("hello javascript")); //如果完全不包含数字,则返回NaN - Not a Number
  document.write("<br>");

</script>
"); window.frames["iframe1085"].document.write(decodeHtml(code1085)); window.frames["iframe1085"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1085"]).load(function(){ $("#iframe1085").height($("#iframe1085").contents().find("body").height()+showittryitheight); }); $("#iframe1085").height($("#iframe1085").contents().find("body").height()+showittryitheight); alreadyWriteCode1085 = code1085; $("#rendering1085").hide(); $("#rendered1085").show(); } var tRereshRetry2DemoPanel1085 = setInterval(rereshRetry2DemoPanel1085,1000); var binded1085 = false; $("textarea#stepcodeTextarea1085").keyup(function(){ if(!binded1085){ $(window).bind('beforeunload',function(){ binded1085 = true; return "xxxx"; }); } var newCode = $(this).val() code1085 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1085!=newCode){ // window.frames["iframe1085"].document.write("
"); // window.frames["iframe1085"].document.write(decodeHtml($("textarea#stepcodeTextarea1085").val())); // window.frames["iframe1085"].document.close(); // $(window.frames["iframe1085"]).load(function(){ // $("#iframe1085").height($("#iframe1085").contents().find("body").height()+showittryitheight); // }); // code1085 = newCode; // } }); $(".tryButton1085").click(function(){ $("#tryDiv1085").show(); $("#stepcodeTextarea1085").focus(); $("#stepcodeTextarea1085").height(200); $("#iframe1085").height(0); window.frames["iframe1085"].document.write("
"); window.frames["iframe1085"].document.write(decodeHtml($("textarea#stepcodeTextarea1085").val())); window.frames["iframe1085"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1085"]).load(function(){ $("#iframe1085").height($("#iframe1085").contents().find("body").height()+showittryitheight); }); $("#iframe1085").height($("#iframe1085").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1085.focus(); editor1085.setSize(null, "250"); $("#rendering1085").hide(); $("#rendered1085").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 editor1085 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1085"), { 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); } } }); editor1085.on("change",function(doc){ if(!binded1085){ $(window).bind('beforeunload',function(){ binded1085 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1085 = newCode; $("textarea#stepcodeTextarea1085").val(newCode); if(alreadyWriteCode1085!=code1085){ lastModifedTime1085 = new Date().getTime(); $("#rendering1085").show(); $("#rendered1085").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1085 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1085.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1085").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

转换为Boolean

使用内置函数Boolean() 转换为Boolean值
当转换字符串时:
非空即为true
当转换数字时:
非0即为true
当转换对象时:
非null即为true
"); window.frames["iframe_show1086"].document.write(decodeHtml($("textarea#stepcodeTextarea1086").val())); window.frames["iframe_show1086"].document.close(); $(window.frames["iframe_show1086"]).load(function(){ $("#iframe_show1086").height($("#iframe_show1086").contents().find("body").height()+showittryitheight); }); $("#iframe_show1086").height($("#iframe_show1086").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> document.write("空字符串''转换为布尔后的值:"+Boolean("")); //空字符串 document.write("<br>"); document.write("非空字符'hello javascript '串转换为布尔后的值:"+Boolean("hello javascript")); //非空字符串 document.write("<br>"); document.write("数字 0 转换为布尔后的值:"+Boolean(0)); //0 document.write("<br>"); document.write("数字 3.14 转换为布尔后的值:"+Boolean(3.14)); //非0 document.write("<br>"); document.write("空对象 null 转换为布尔后的值:"+Boolean(null)); //null document.write("<br>"); document.write("非空对象 new Object() 转换为布尔后的值:"+Boolean(new Object())); //对象存在 document.write("<br>"); </script>
<script>
  document.write("空字符串''转换为布尔后的值:"+Boolean(""));  //空字符串
  document.write("<br>");
  document.write("非空字符'hello javascript '串转换为布尔后的值:"+Boolean("hello javascript"));  //非空字符串
  document.write("<br>");
  document.write("数字 0 转换为布尔后的值:"+Boolean(0));  //0
  document.write("<br>");
  document.write("数字 3.14 转换为布尔后的值:"+Boolean(3.14)); //非0 
  document.write("<br>");
  document.write("空对象 null 转换为布尔后的值:"+Boolean(null));  //null
  document.write("<br>");
  document.write("非空对象 new Object() 转换为布尔后的值:"+Boolean(new Object()));  //对象存在
  document.write("<br>");

</script>
"); window.frames["iframe1086"].document.write(decodeHtml(code1086)); window.frames["iframe1086"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1086"]).load(function(){ $("#iframe1086").height($("#iframe1086").contents().find("body").height()+showittryitheight); }); $("#iframe1086").height($("#iframe1086").contents().find("body").height()+showittryitheight); alreadyWriteCode1086 = code1086; $("#rendering1086").hide(); $("#rendered1086").show(); } var tRereshRetry2DemoPanel1086 = setInterval(rereshRetry2DemoPanel1086,1000); var binded1086 = false; $("textarea#stepcodeTextarea1086").keyup(function(){ if(!binded1086){ $(window).bind('beforeunload',function(){ binded1086 = true; return "xxxx"; }); } var newCode = $(this).val() code1086 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1086!=newCode){ // window.frames["iframe1086"].document.write("
"); // window.frames["iframe1086"].document.write(decodeHtml($("textarea#stepcodeTextarea1086").val())); // window.frames["iframe1086"].document.close(); // $(window.frames["iframe1086"]).load(function(){ // $("#iframe1086").height($("#iframe1086").contents().find("body").height()+showittryitheight); // }); // code1086 = newCode; // } }); $(".tryButton1086").click(function(){ $("#tryDiv1086").show(); $("#stepcodeTextarea1086").focus(); $("#stepcodeTextarea1086").height(200); $("#iframe1086").height(0); window.frames["iframe1086"].document.write("
"); window.frames["iframe1086"].document.write(decodeHtml($("textarea#stepcodeTextarea1086").val())); window.frames["iframe1086"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1086"]).load(function(){ $("#iframe1086").height($("#iframe1086").contents().find("body").height()+showittryitheight); }); $("#iframe1086").height($("#iframe1086").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1086.focus(); editor1086.setSize(null, "250"); $("#rendering1086").hide(); $("#rendered1086").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 editor1086 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1086"), { 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); } } }); editor1086.on("change",function(doc){ if(!binded1086){ $(window).bind('beforeunload',function(){ binded1086 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1086 = newCode; $("textarea#stepcodeTextarea1086").val(newCode); if(alreadyWriteCode1086!=code1086){ lastModifedTime1086 = new Date().getTime(); $("#rendering1086").show(); $("#rendered1086").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1086 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1086.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1086").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

Number()和parseInt()的区别

Number()和parseInt()一样,都可以用来进行数字的转换
区别在于,当转换的内容包含非数字的时候,Number() 会返回NaN(Not a Number)
parseInt() 要看情况,如果以数字开头,就会返回开头的合法数字部分,如果以非数字开头,则返回NaN
"); window.frames["iframe_show1087"].document.write(decodeHtml($("textarea#stepcodeTextarea1087").val())); window.frames["iframe_show1087"].document.close(); $(window.frames["iframe_show1087"]).load(function(){ $("#iframe_show1087").height($("#iframe_show1087").contents().find("body").height()+showittryitheight); }); $("#iframe_show1087").height($("#iframe_show1087").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> document.write("通过Number() 函数转换字符串'123' 后得到的数字:"+Number("123")); //正常的 document.write("<br>"); document.write("通过Number() 函数转换字符串'123abc' 后得到的数字:"+Number("123abc")); //包含非数字 document.write("<br>"); document.write("通过Number() 函数转换字符串'abc123' 后得到的数字:"+Number("abc123")); //包含非数字 document.write("<br>"); document.write("通过parseInt() 函数转换字符串'123' 后得到的数字:"+parseInt("123")); //正常的 document.write("<br>"); document.write("通过parseInt() 函数转换字符串'123abc' 后得到的数字:"+parseInt("123abc")); //包含非数字,返回开头的合法数字部分 document.write("<br>"); document.write("通过parseInt() 函数转换字符串'abc123' 后得到的数字:"+parseInt("abc123")); //包含非数字,以非数字开头,返回NaN document.write("<br>"); </script>
<script>
  document.write("通过Number() 函数转换字符串'123' 后得到的数字:"+Number("123"));   //正常的
  document.write("<br>");
  document.write("通过Number() 函数转换字符串'123abc' 后得到的数字:"+Number("123abc"));   //包含非数字
  document.write("<br>");
  document.write("通过Number() 函数转换字符串'abc123' 后得到的数字:"+Number("abc123"));   //包含非数字
  document.write("<br>");

  document.write("通过parseInt() 函数转换字符串'123' 后得到的数字:"+parseInt("123"));   //正常的
  document.write("<br>");
  document.write("通过parseInt() 函数转换字符串'123abc' 后得到的数字:"+parseInt("123abc"));   //包含非数字,返回开头的合法数字部分
  document.write("<br>");
  document.write("通过parseInt() 函数转换字符串'abc123' 后得到的数字:"+parseInt("abc123"));   //包含非数字,以非数字开头,返回NaN
  document.write("<br>");

</script>
"); window.frames["iframe1087"].document.write(decodeHtml(code1087)); window.frames["iframe1087"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1087"]).load(function(){ $("#iframe1087").height($("#iframe1087").contents().find("body").height()+showittryitheight); }); $("#iframe1087").height($("#iframe1087").contents().find("body").height()+showittryitheight); alreadyWriteCode1087 = code1087; $("#rendering1087").hide(); $("#rendered1087").show(); } var tRereshRetry2DemoPanel1087 = setInterval(rereshRetry2DemoPanel1087,1000); var binded1087 = false; $("textarea#stepcodeTextarea1087").keyup(function(){ if(!binded1087){ $(window).bind('beforeunload',function(){ binded1087 = true; return "xxxx"; }); } var newCode = $(this).val() code1087 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1087!=newCode){ // window.frames["iframe1087"].document.write("
"); // window.frames["iframe1087"].document.write(decodeHtml($("textarea#stepcodeTextarea1087").val())); // window.frames["iframe1087"].document.close(); // $(window.frames["iframe1087"]).load(function(){ // $("#iframe1087").height($("#iframe1087").contents().find("body").height()+showittryitheight); // }); // code1087 = newCode; // } }); $(".tryButton1087").click(function(){ $("#tryDiv1087").show(); $("#stepcodeTextarea1087").focus(); $("#stepcodeTextarea1087").height(200); $("#iframe1087").height(0); window.frames["iframe1087"].document.write("
"); window.frames["iframe1087"].document.write(decodeHtml($("textarea#stepcodeTextarea1087").val())); window.frames["iframe1087"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1087"]).load(function(){ $("#iframe1087").height($("#iframe1087").contents().find("body").height()+showittryitheight); }); $("#iframe1087").height($("#iframe1087").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1087.focus(); editor1087.setSize(null, "250"); $("#rendering1087").hide(); $("#rendered1087").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 editor1087 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1087"), { 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); } } }); editor1087.on("change",function(doc){ if(!binded1087){ $(window).bind('beforeunload',function(){ binded1087 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1087 = newCode; $("textarea#stepcodeTextarea1087").val(newCode); if(alreadyWriteCode1087!=code1087){ lastModifedTime1087 = new Date().getTime(); $("#rendering1087").show(); $("#rendered1087").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1087 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1087.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1087").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

String()和toString()的区别

String()和toString()一样都会返回字符串,区别在于对null的处理
String()会返回字符串"null"
toString() 就会报错,无法执行
"); window.frames["iframe_show1088"].document.write(decodeHtml($("textarea#stepcodeTextarea1088").val())); window.frames["iframe_show1088"].document.close(); $(window.frames["iframe_show1088"]).load(function(){ $("#iframe_show1088").height($("#iframe_show1088").contents().find("body").height()+showittryitheight); }); $("#iframe_show1088").height($("#iframe_show1088").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var a = null; document.write('String(null) 把空对象转换为字符串:'+String(a)); document.write("<br>"); document.write('null.toString() 就会报错,所以后面的代码不能执行'); document.write(a.toString()); document.write("因为第5行报错,所以这一段文字不会显示"); </script>
<script>
  var a = null;
  document.write('String(null) 把空对象转换为字符串:'+String(a));  
  document.write("<br>");  
  document.write('null.toString() 就会报错,所以后面的代码不能执行');  
  document.write(a.toString());  
  document.write("因为第5行报错,所以这一段文字不会显示");  
</script>
"); window.frames["iframe1088"].document.write(decodeHtml(code1088)); window.frames["iframe1088"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1088"]).load(function(){ $("#iframe1088").height($("#iframe1088").contents().find("body").height()+showittryitheight); }); $("#iframe1088").height($("#iframe1088").contents().find("body").height()+showittryitheight); alreadyWriteCode1088 = code1088; $("#rendering1088").hide(); $("#rendered1088").show(); } var tRereshRetry2DemoPanel1088 = setInterval(rereshRetry2DemoPanel1088,1000); var binded1088 = false; $("textarea#stepcodeTextarea1088").keyup(function(){ if(!binded1088){ $(window).bind('beforeunload',function(){ binded1088 = true; return "xxxx"; }); } var newCode = $(this).val() code1088 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1088!=newCode){ // window.frames["iframe1088"].document.write("
"); // window.frames["iframe1088"].document.write(decodeHtml($("textarea#stepcodeTextarea1088").val())); // window.frames["iframe1088"].document.close(); // $(window.frames["iframe1088"]).load(function(){ // $("#iframe1088").height($("#iframe1088").contents().find("body").height()+showittryitheight); // }); // code1088 = newCode; // } }); $(".tryButton1088").click(function(){ $("#tryDiv1088").show(); $("#stepcodeTextarea1088").focus(); $("#stepcodeTextarea1088").height(200); $("#iframe1088").height(0); window.frames["iframe1088"].document.write("
"); window.frames["iframe1088"].document.write(decodeHtml($("textarea#stepcodeTextarea1088").val())); window.frames["iframe1088"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1088"]).load(function(){ $("#iframe1088").height($("#iframe1088").contents().find("body").height()+showittryitheight); }); $("#iframe1088").height($("#iframe1088").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1088.focus(); editor1088.setSize(null, "250"); $("#rendering1088").hide(); $("#rendered1088").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 editor1088 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1088"), { 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); } } }); editor1088.on("change",function(doc){ if(!binded1088){ $(window).bind('beforeunload',function(){ binded1088 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1088 = newCode; $("textarea#stepcodeTextarea1088").val(newCode); if(alreadyWriteCode1088!=code1088){ lastModifedTime1088 = new Date().getTime(); $("#rendering1088").show(); $("#rendered1088").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1088 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1088.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1088").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

上传截图