标签:fun 删除 turn 长度 for pre string 函数 分割
切割字符串
<script type="text/javascript"> var arr=["张三","中意","李四"]; var str=arr[0]; var sub="^i切.割^" for (var i = 1; i < arr.length; i++) { str=str+sub+arr[i] } console.log(str) </script>
函数的两种方式
<script type="text/javascript"> function f1(a,b){ return a+b }; var f2=function(a,b){ return a+b }; console.log(f1("函数", "声明")); console.log(f2("函数", "表达式")); </script> //关于在函数前调用的问题是因为 函数声明提升即解析机制问题 <script type="text/javascript"> console.log(f1("函数", "声明")); console.log(f2("函数", "表达式")); function f1(a,b){ return a+b }; var f2=function(a,b){ return a+b }; </script>
函数的递归
<script type="text/javascript"> function f1(){ console.log("递归"); }; f1() </script> //同样的将f1放在函数自身内部调用自己如果不添加条件 就会不停的自调用 这就是死递归 <script type="text/javascript"> function f1(){ console.log("递归"); f1() }; </script>
数组检测
<script type="text/javascript"> var arr = [1, 2, 3, 4, 5]; console.log(arr); console.log(arr.valueOf()); console.log(arr.toString()); console.log(arr.join()); console.log(arr.join("-")); console.log(arr.join("")); //检测数组 console.log(arr instanceof Array);//true //console.log({} instanceof Array);//false console.log(Array.isArray(arr));//true //console.log(Array.isArray({}));//false </script>
数组的增删
var arr = [1, 2, 3, 4, 5]; console.log(arr); //console.log(arr.push(0));//从后面添加元素 返回新数组的长度 //console.log(arr.pop());//从后面删除元素 返回被删除的元素 //console.log(arr.shift());//从前面删除元素 返回被删除的元素 //console.log(arr.unshift(0));//从前面添加元素 返回新数组的长度 console.log(arr);
数组的分割(join("用什么分割"))
//将一个字符串数组输出为用|分割的字符串,如 ["1", "2", "3"] 变为 "1|2|3"。(两种方式) var arr = ["1", "2", "3"]; /*function join(arr, sep) { var str = arr[0]; for (var i = 1; i < arr.length; i++) { str = str + sep + arr[i]; } return str; } console.log(join(arr, "|"));*/ console.log(arr.join("|"));
数组的翻转(reverse)
//将字符串数组的顺序进行反转。["a","b","c","d"]变为["d","c","b","a"]。(三种方式) var arr = ["a", "b", "c", "d"]; console.log(arr); /*function reverse(arr) { var newArr = []; for (var i = arr.length - 1; i >= 0; i--) { newArr.push(arr[i]); } //console.log(newArr); return newArr; } console.log(reverse(arr));*/ /*function reverse(arr) { for (var i = 0; i < arr.length / 2; i++) { //0 arr.length-1-0 //1 arr.length-1-1 //2 arr.length-1-2 //i arr.length-1-i var temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } //console.log(arr); return arr; } console.log(reverse(arr));*/ //console.log(arr.reverse());
标签:fun 删除 turn 长度 for pre string 函数 分割
原文地址:http://www.cnblogs.com/2026tu/p/7944349.html