how2j.cn


工具版本兼容问题
JavaScript中的对象是有着属性和方法的一种特殊数据类型。

常见的对象有数字Number,字符串String,日期Date,数组Array等。

本章节从数字对象开始讲起

注: 这里讲的Number是对象Number,基本数据类型中的基本类型Number是不一样的Number。

关键字 简介 示例代码
new Number
创建一个数字对象
示例代码
属性MIN_VALUE
属性MAX_VALUE
最小值 最大值
示例代码
属性NaN
表示不是数字
示例代码
方法toFixed
返回一个数字的小数表达
示例代码
方法toExponential
返回一个数字的科学计数法表达
示例代码
方法valueOf
返回一个数字对象的基本数字类型
示例代码
练习-乘法
示例代码
答案-乘法
示例代码
示例 1 : 创建一个数字对象   
示例 2 : 最小值 最大值   
示例 3 : 表示不是数字   
示例 4 : 返回一个数字的小数表达   
示例 5 : 返回一个数字的科学计数法表达   
示例 6 : 返回一个数字对象的基本数字类型   
示例 7 : 练习-乘法   
示例 8 : 答案-乘法   

示例 1 :

创建一个数字对象

可以通过new Number()创建一个数字对象

与基本类型的数字不同,对象类型的数字,拥有更多的属性和方法
接下来就会讲解各种属性和方法
"); window.frames["iframe_show1116"].document.write(decodeHtml($("textarea#stepcodeTextarea1116").val())); window.frames["iframe_show1116"].document.close(); $(window.frames["iframe_show1116"]).load(function(){ $("#iframe_show1116").height($("#iframe_show1116").contents().find("body").height()+showittryitheight); }); $("#iframe_show1116").height($("#iframe_show1116").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var x = new Number(123); document.write('数字对象x的值:'+x); document.write("<br>"); document.write('数字对象x的类型:'+typeof x); //通过typeof 获知这是一个object document.write("<br>"); var y = 123; document.write('基本类型y的值:'+y); document.write("<br>"); document.write('基本类型y的类型:'+typeof y); //通过typeof 获知这是一个number </script>
<script>

var x = new Number(123);
 document.write('数字对象x的值:'+x);
 document.write("<br>");
 document.write('数字对象x的类型:'+typeof x); //通过typeof 获知这是一个object
 document.write("<br>");
var y = 123;
 document.write('基本类型y的值:'+y);
 document.write("<br>");
 document.write('基本类型y的类型:'+typeof y); //通过typeof 获知这是一个number

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


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

最小值 最大值

Number对象能够取到的最大和最小边界
"); window.frames["iframe_show1118"].document.write(decodeHtml($("textarea#stepcodeTextarea1118").val())); window.frames["iframe_show1118"].document.close(); $(window.frames["iframe_show1118"]).load(function(){ $("#iframe_show1118").height($("#iframe_show1118").contents().find("body").height()+showittryitheight); }); $("#iframe_show1118").height($("#iframe_show1118").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> document.write('Number对象的最小值:'+Number.MIN_VALUE); document.write("<br>"); document.write('Number对象的最大值:'+Number.MAX_VALUE); </script>
<script>

 document.write('Number对象的最小值:'+Number.MIN_VALUE);

 document.write("<br>");

 document.write('Number对象的最大值:'+Number.MAX_VALUE);

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


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

表示不是数字

NaN(Not a Number),表示不是一个数字
当通过非数字创建Number的时候,就会得到NaN.
注意: 不能通过 是否等于Number.NaN来判断 是否 “不是一个数字”,应该使用函数 isNaN()
"); window.frames["iframe_show1122"].document.write(decodeHtml($("textarea#stepcodeTextarea1122").val())); window.frames["iframe_show1122"].document.close(); $(window.frames["iframe_show1122"]).load(function(){ $("#iframe_show1122").height($("#iframe_show1122").contents().find("body").height()+showittryitheight); }); $("#iframe_show1122").height($("#iframe_show1122").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function p(s){ document.write(s); document.write("<br>"); } var a = new Number("123abc"); //通过非数字创建Number对象,得到的是一个NaN p('通过非数字字符串"123abc"创建出来的Number对象 a的值是:'+a); p('但是, a==Number.NaN会返回:'+(a==Number.NaN)); //即便是一个NaN 也"不等于" Number.NaN p('正确判断是否是NaN的方式是调用isNaN函数:'+isNaN(a)); //正确的方式是通过isNaN() 函数进行判断 </script>
<script>
function p(s){
  document.write(s);
  document.write("<br>");
}

var a = new Number("123abc"); //通过非数字创建Number对象,得到的是一个NaN
p('通过非数字字符串"123abc"创建出来的Number对象 a的值是:'+a);

p('但是, a==Number.NaN会返回:'+(a==Number.NaN)); //即便是一个NaN 也"不等于" Number.NaN

p('正确判断是否是NaN的方式是调用isNaN函数:'+isNaN(a)); //正确的方式是通过isNaN() 函数进行判断

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


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

返回一个数字的小数表达

toFixed返回一个数字的小数表达
"); window.frames["iframe_show1119"].document.write(decodeHtml($("textarea#stepcodeTextarea1119").val())); window.frames["iframe_show1119"].document.close(); $(window.frames["iframe_show1119"]).load(function(){ $("#iframe_show1119").height($("#iframe_show1119").contents().find("body").height()+showittryitheight); }); $("#iframe_show1119").height($("#iframe_show1119").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function p(s){ document.write(s); document.write("<br>"); } var a = new Number("123"); p("数字对象123通过toFixed(2) 保留两位小数:"+a.toFixed(2)); //保留两位小数点 var b = new Number("3.1415926"); p("PI 通过toFixed(3) 保留三位小数:"+b.toFixed(3));//保留三位小数点 </script>
<script>
function p(s){
  document.write(s);
  document.write("<br>");
}

var a = new Number("123"); 

p("数字对象123通过toFixed(2) 保留两位小数:"+a.toFixed(2)); //保留两位小数点

var b = new Number("3.1415926"); 

p("PI 通过toFixed(3) 保留三位小数:"+b.toFixed(3));//保留三位小数点

</script>

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


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

返回一个数字的科学计数法表达

返回一个Number对象的科学计数法表达
"); window.frames["iframe_show1120"].document.write(decodeHtml($("textarea#stepcodeTextarea1120").val())); window.frames["iframe_show1120"].document.close(); $(window.frames["iframe_show1120"]).load(function(){ $("#iframe_show1120").height($("#iframe_show1120").contents().find("body").height()+showittryitheight); }); $("#iframe_show1120").height($("#iframe_show1120").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function p(s){ document.write(s); document.write("<br>"); } var a = new Number("123"); p("数字对象123通过toExponential 返回计数法表达 "+a.toExponential ()); var b = new Number("3.1415926"); p("数字对象3.1415926通过toExponential 返回计数法表达 "+b.toExponential ()); </script>
<script>
function p(s){
  document.write(s);
  document.write("<br>");
}
 
var a = new Number("123"); 
 
p("数字对象123通过toExponential 返回计数法表达 "+a.toExponential ()); 
 
var b = new Number("3.1415926"); 
 
p("数字对象3.1415926通过toExponential 返回计数法表达 "+b.toExponential ());
 
</script>
"); window.frames["iframe1120"].document.write(decodeHtml(code1120)); window.frames["iframe1120"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1120"]).load(function(){ $("#iframe1120").height($("#iframe1120").contents().find("body").height()+showittryitheight); }); $("#iframe1120").height($("#iframe1120").contents().find("body").height()+showittryitheight); alreadyWriteCode1120 = code1120; $("#rendering1120").hide(); $("#rendered1120").show(); } var tRereshRetry2DemoPanel1120 = setInterval(rereshRetry2DemoPanel1120,1000); var binded1120 = false; $("textarea#stepcodeTextarea1120").keyup(function(){ if(!binded1120){ $(window).bind('beforeunload',function(){ binded1120 = true; return "xxxx"; }); } var newCode = $(this).val() code1120 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1120!=newCode){ // window.frames["iframe1120"].document.write("
"); // window.frames["iframe1120"].document.write(decodeHtml($("textarea#stepcodeTextarea1120").val())); // window.frames["iframe1120"].document.close(); // $(window.frames["iframe1120"]).load(function(){ // $("#iframe1120").height($("#iframe1120").contents().find("body").height()+showittryitheight); // }); // code1120 = newCode; // } }); $(".tryButton1120").click(function(){ $("#tryDiv1120").show(); $("#stepcodeTextarea1120").focus(); $("#stepcodeTextarea1120").height(200); $("#iframe1120").height(0); window.frames["iframe1120"].document.write("
"); window.frames["iframe1120"].document.write(decodeHtml($("textarea#stepcodeTextarea1120").val())); window.frames["iframe1120"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1120"]).load(function(){ $("#iframe1120").height($("#iframe1120").contents().find("body").height()+showittryitheight); }); $("#iframe1120").height($("#iframe1120").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1120.focus(); editor1120.setSize(null, "250"); $("#rendering1120").hide(); $("#rendered1120").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 editor1120 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1120"), { 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); } } }); editor1120.on("change",function(doc){ if(!binded1120){ $(window).bind('beforeunload',function(){ binded1120 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1120 = newCode; $("textarea#stepcodeTextarea1120").val(newCode); if(alreadyWriteCode1120!=code1120){ lastModifedTime1120 = new Date().getTime(); $("#rendering1120").show(); $("#rendered1120").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1120 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1120.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1120").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

返回一个数字对象的基本数字类型

方法 valueOf() 返回一个基本类型的数字
通过typeof 判断数据类型可以发现,一种是object,一种是number
"); window.frames["iframe_show1121"].document.write(decodeHtml($("textarea#stepcodeTextarea1121").val())); window.frames["iframe_show1121"].document.close(); $(window.frames["iframe_show1121"]).load(function(){ $("#iframe_show1121").height($("#iframe_show1121").contents().find("body").height()+showittryitheight); }); $("#iframe_show1121").height($("#iframe_show1121").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function p(s){ document.write(s); document.write("<br>"); } var a = new Number("123"); var b = a.valueOf(); p('数字对象a的类型是: '+typeof a); //返回object p('通过valueOf()返回的值的类型是'+typeof b); //返回number </script>
<script>
function p(s){
  document.write(s);
  document.write("<br>");
}

var a = new Number("123"); 

var b = a.valueOf();

p('数字对象a的类型是: '+typeof a); //返回object
p('通过valueOf()返回的值的类型是'+typeof b); //返回number

</script>

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


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

练习-乘法

练习难度
Or  姿势不对,事倍功半! 点击查看做练习的正确姿势
参考阶段性练习1的做法,做一个乘法计算器,但是要求输出结果是科学计数法表示
练习-乘法
示例 8 :

答案-乘法

在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多


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


关于 前端部分-JavaScript-数字 的提问

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

上传截图