【JS学习笔记】JS基础上:数据类型

来源:互联网 发布:cf出现网络异常 编辑:程序博客网 时间:2024/06/06 00:21

晓石头的博客
邮箱:178673693@qq.com
转载请注明出处,原文链接:http://blog.csdn.net/qiulanzhu/article/details/50544482


index.html

--------------------------------

<!DOCTYPE HEML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <meta http-equiv="Content-Type" content="text/html;charset=gb2312"/>   <title>使用JS</title>   <script type="text/javascript" src="demo.js"></script> </head> <body>  </body></html>


demo.js

--------------------------------

/*1.中文乱码文件保存格式ANSI;网页设置格式charset=gb2312。*/(function(){document.write("<br>---------------------数字------------------------<br>");//数字型讲解//数字型转字符串型var num = 10;var str = Number.toString(num);document.write(typeof(str) == "string"); document.write("<br>");//四舍五入var num1 = 3.14159265;var num2 = num1.toFixed(2);document.write(num2);document.write("<br>");var num3 = num1.toFixed(4);document.write(num3);document.write("<br>");//返回前几位数字var num4 = num1.toPrecision(4);document.write(num4);document.write("<br>");//Math:介绍一些方法//四舍五入取整:rounddocument.write(Math.round(5.53));document.write("<br>");//舍弃去整document.write(Math.floor(5.53));document.write("<br>");//参数0-1和0-10的随机数document.write(Math.random());document.write("<br>");document.write(Math.round( (Math.random()*10) ));document.write("<br>");document.write("<br>---------------------字符串------------------------<br>");//字符串讲解//属性:length,indexof,substring,charAt(整数)var str1 = "qiuyi";document.write(str1.length);document.write("<br>");document.write(str1.indexOf("yi")+"<br>");document.write(str1.substring(3)+"<br>");document.write(str1.charAt(0)+"<br>");//String转Numbervar str2 = "3.14";var num = Number(str2);document.write(typeof(num) == "number");document.write("<br>");document.write(num +"<br>");document.write(str1 - 1 +"<br>");//NaN:非数值document.write(str2 - 1 +"<br>");//减号:隐式转换,将String转Numberdocument.write(str2 + 1 +"<br>");//加号: 在字符串后面追加document.write("<br>---------------------布尔------------------------<br>");//布尔型讲解var m = "";var n = {};var o = [];var p = false;var q = undefined;var r = null;if(!m){document.write("\"\" is false<br>");}if(!n){document.write("{} is false<br>");}if(!o){document.write("[] is false<br>");}if(!p){document.write("false is false<br>");}if(!q){document.write("undefined is false<br>");}if(!r){document.write("null is false<br>");}document.write("<br>---------------------复合类型------------------------<br>");/*1、数组var arr = new Array();属性:constructor:返回对创建对象的数组的函数引用indexinputlength方法:concat:合并join:把数组按照一定的格式进行串联push:数组的追加pop:删除数组返回的最后一个元素sorttoStringshift:删除并且返回数组的第一个元素*/var arr = new Array();arr.push(1);arr.push(3);arr.push(5);arr.push(7);arr.push(9);document.write(arr.length +"<br>");var arr1 = [1,2,3,4,5,6];document.write(arr1.join("-") +"<br>");document.write(arr.concat(arr1).toString() +"<br>");//toString()默认用,分开//遍历for(var i=0; i<arr1.length; i++){document.write(arr1[i] +" ");}document.write("<br>");//Array.each原型遍历Array.each = function(array, fn){for(var i=0; i<arr1.length; i++){fn(array[i]);}}Array.each(arr1,function(v){document.write(v +" ");})document.write("<br>");/*2、特殊对象---函数(function)*/document.write("<br>---------------------特殊值------------------------<br>");/*1、null 不是有效的对象/数组/数子/字符串  他们为空2、undefined 他是代表没有定义 和空不是一个概念[没有] 但是有容器,有一个盒子,但是盒子是空的undefined 连盒子都没有*/document.write("<br>---------------------内置特殊对象------------------------<br>");/*Data对象Error错误对象ReExp对象*/})()


0 0