晓石头的博客
邮箱:178673693@qq.com
转载请注明出处,原文链接:http://blog.youkuaiyun.com/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:介绍一些方法
//四舍五入取整:round
document.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转Number
var 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转Number
document.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:返回对创建对象的数组的函数引用
index
input
length
方法:
concat:合并
join:把数组按照一定的格式进行串联
push:数组的追加
pop:删除数组返回的最后一个元素
sort
toString
shift:删除并且返回数组的第一个元素
*/
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对象
*/
})()