函数实参集合 arguments
arguments 是一个类数组 不能直接使用数组中的方法 有索引 length 可以进行for 循环
任意数求和
function Sum(){
<!--设置初始值-->
var total = 0;
<!--遍历arguments-->
for(var i = 0; i<arguments.length;i++){
var item = arguments[i];
if(typeof item !== 'number'){
item = parseFloat(item)
}else if(!isNaN(item){
total +=item;
}else{
console.log('错误')!
}
}
return total
}
var r = Sum(1,13,13,14,15)
console.log(r1);
代码的健壮性 允许一些意外的情况发生时 程序还能正常运行 增加出现意外的处理
不定参数--- 和arguments 相似的功能
...形参变量 展开运算符
函数表达式
- 函数表达式
- 把函数当做一个值赋给变量或者对象的属性
var a = function(){ console.log('函数表达式') } a(); box.onclick = function(){ alert('事件属性') }- 普通对象属性
var obj = { name:function(){ console.log('张三') } } obj.getName() - 自执行函数 创建闭包最简单的方法
(function(a,b){
console.log(a,b)
})(1,2)
~function(x,y){ //按位取反
console.log(x,y)
}(1,2)
函数递归 分为两部分 在函数体内部 调用函数自身 达到重复某一行为的目的
递归和for循环一样 要考虑好递归终止条件 否则会出现死循环
function Sum(num){
if(num === 10){
return 0
}
if(num %3 === 0){
return num + Sum(num + 1)
}else{
return Sum(num + 1)
}
}
console.log(Sum(1))
数组方法
| 语法 | 参数 | 返回值 | 作用 | 是否改变原数组 |
|---|---|---|---|---|
| push | 添加的项 | 添加后数组的长度 | 末尾添加项 | 是 |
| pop | 无 | 被删除的数组项 | 删除末尾一项 | 是 |
| unshift | 添加的项 | 添加后数组的长度 | 开头添加项 | 是 |
| shift | 无 | 被删除的项 | 删除起始的一项 | 是 |
| splice(n,m) | 起始索引 n 要删除的个数m | 删除项组成的新数组 | 从索引n开始 删除m个 | 是 |
| splice(n,m,x) | 起始索引 n 删除m个 替换部分 x | 被删除项组成的新数组 | 从索引n开始 删除m个 再用x替换 | 是 |
| splice(n,0,x) | 在索引n 添加x部分 | 空数组 | 在索引n前面添加x部分 | 是 |
数组复制 拼接
| 语法 | 参数 | 返回值 | 作用 | 是否改变原数组 |
|---|---|---|---|---|
| slice(n,m) | 起始索引 m 终点索引 m | 复制的项组成的新数组 | 从索引n复制到索引m(不包含m) | 否 |
| concat () | 需要拼接的数组或数组项 | 拼接后的新数组 | 数组拼接 | 否 |
数组转成字符串
| 语法 | 参数 | 返回值 | 作用 | 是否改变原数组 |
|---|---|---|---|---|
| join() | 分隔符 默认空字符串 | 拼接后的字符串 | 参数指定的分隔符把数组链接成字符串 | 否 |
| toString() | 无 | 字符串 | 把数组转换成字符串 | 否 |
数组项在数组中是否出现过
| 语法 | 参数 | 返回值 | 作用 | 是否改变原数组 |
|---|---|---|---|---|
| indexOf(x) | 数组项x | 存在x,就返回第一次出现的索引 不存在 返回 -1 | 数组项在数组中第一次出现的位置 | 否 |
| lastIndexOf(x) | 数组项x | 存在x,就返回最后一次出现的索引 不存在 返回 -1 | 数组项在数组中最后一次出现的位置 | 否 |
数组排序
| 语法 | 参数 | 返回值 | 作用 | 是否改变原数组 |
|---|---|---|---|---|
| sort(function(a,b){return a - b}) | 回调函数 | 升序排列的数组 | 升序排列 | 是 |
| sort(function(a,b){return b - a}) | 回调函数 | 降序排列的数组 | 升降序列 | 是 |
| reverse() | 无 | 翻转过来的数组 | 让数组翻转过来 | 是 |
数组遍历
| 语法 | 参数 | 返回值 | 作用 | 是否改变原数组 |
|---|---|---|---|---|
| forEach(function(item,index){回调函数}) | item遍历的每个数组项 index数组项对应的索引 | 无 | 遍历数组 | 否 |
| map(function(item,index){}) | item遍历的每个数组项 index数组项对应的索引 | 回调函数组成的新数组 | 将原数组映射成一个新数组 | 否 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>数组方法</title>
</head>
<body>
</body>
</html>
<script>
var ary = [1,2,3,4,5];
var a = ary.push(11,12);
console.log(a); // 7添加后数组的长度
console.log(ary); //1,2,3,4,5,11,12
var b = ary.pop();
console.log(b); //12 删除的那一项
console.log(ary); //1,2,3,4,5,11
var c = ary.unshift(999);
console.log(c); // 7添加后数组的长度
console.log(ary); //999,1,2,3,4,5,11
var d = ary.shift();
console.log(d); // 999 删除的那一项
console.log(ary); //1,2,3,4,5,11
var e = ary.splice(2,3);
console.log(e); //3,4,5 删除的项
console.log(ary); //1,2,11 剩余的项
var f = ary.splice(1,1,999,666);
console.log(f); //[2] 删除的项
console.log(ary); //1,999,666,444
var g = ary.splice(2,0,444);
console.log(g); //[]
console.log(ary); //1,2,444,999,666
var h = ary.slice(2,4);
console.log(h); //[444,666]
console.log(ary); //[1,999,444,666,11]
var i = ary.concat([1,2,3,4]);
console.log(i); //1,999,444,666,11,1,2,3,4
console.log(ary);//1,999,444,666,11
var j = ary.join('+')
console.log(j); //1+999+444+666+11
console.log(ary); //1,999,444,666,11
var k = ary.toString();
console.log(k); //1,999,444,666,11
console.log(ary); //[1,999,444,666,11]
var m = ary.forEach(function (item,index) {
console.log(item);
})
console.log(ary);
var q = ary.map(function(item,index){
console.log(item);
})
console.log(ary);
</script>
数组去重
- 双for循环去重 一次拿出数组中的每一项 和当前取出的后面那一项比较 若重复 就删除
- 对象去重
- 创建一个空对象 遍历数组中的每一项 把数组中每一项存储的值 当做键值对存储起来 做一次判断 判断当前是否存在这个属性 不是undefined 就说名重复了 再删除 存在且在项目组中没有出现过 直接存储
- 遍历数组 把数组项 都存储到对象中 再用 for in 把对象中的数组项取出来 还原成一个数组
- 准备一个空数组 遍历数组 把数组项取出来 判断在新数组中是否出现过 没有 直接push 进去
```
var ary = [1,1,1,2,2,2,3,3,3]
function unique3(ary) {
var newArr = [];
for (var i = 0; i < ary.length; i++) { var item = ary[i];
if (!newArr.includes(item)) {
newArr.push(item)
}
} return newArr; }
```
字符串中常用的方法
- 通过索引获取字符串
- charAt(index) 获取索引为index的字符
- charCodeAt(index) 获取字符串中指定索引位置的字符的Unicode 编码值 返回
- 复制或者截取字符串
- substr(n,m)从索引n 开始截取m个 不写m 直接截取到尾
- substring(n,m)从索引n开始 截取到索引m(不包含m)
- 大小写转换
- toLowerCase() 大写转成小写
- toUpperCase() 小写转换成大写
- 获取指定字符串在字符串中的位置
- indexOf('x')
- lastIndexOf('x')
- split('') 字符串拆分成数组
- 字符串替换 replace(被替换的内容,‘要替换的内容’) 字符串替换
- match(匹配内容或者正则) 匹配到返回一个数组 匹配不到返回null 把一个单词倒过来 var s2 = str.split('').reverse().join('');
Math 对象 浏览器内置的专门处理数学计算的对象
- Math.abs() 取绝对值
- Math.floor() 向下取整
- Math.ceil()向上取整
- Math.round() 四舍五入 负数是四舍六入
- Math.random() 生成一个0~1之间的随机小数
- Math.round(Math.random() * (m - n) + n) 生成一个[n,m]之间的随机数
- Math.aqrt() 获取某个值的算术平方根
- Math.PI 获取圆周率 toFixed(n) 保留几位小数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>随机验证码</title>
<style>
#codeBox {
margin: 30px auto;
height: 100px;
width: 200px;
line-height: 100px;
text-align: center;
font-size: 30px;
border: 10px solid #00b38a;
}
</style>
</head>
<body>
<div id="codeBox"></div>
</body>
</html>
<script>
function validateCode() {
// 准备数据
var strbase = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789';
//准备用来拼接的空字符串
var code = '';
//循环,在循环中获取随机数,然后用这个随即数当做索引取一个字符。
while (code.length < 4) {
var ran = Math.round(Math.random() * (strbase.length - 0) + 0);
var char = strbase[ran];
//在拼接之前需要判断char是否已经在codeBox中出现过了
var upperChar = char.toUpperCase();
var upperCode = code.toUpperCase();
if (upperCode.indexOf(upperChar) === -1) {
code += char;
}
}
return code;
}
var codeBox = document.getElementById('codeBox');
codeBox.innerHTML = validateCode();
</script>