标签:如何 有用 reac type 语法 转换 支持 排序 blog
function a(num){ if(num>3){ a(--num); } console.log(num); } a(5); //如何创建对象的实例 var obj1= new Object(); console.log(obj1); obj1.name="吴琼"; obj1.age=28; console.log(obj1.name+" "+obj1.age); //对象字面量语法 ,有点封装的感觉 var obj2 = { name:"吴琼", age:28 }; var obj3 = { age:28, age_wo:30 }; console.log(obj3["age_wo"]); console.log(obj2.name+" "+obj2.age); function show(arg){ var s=""; if(arg.name != undefined){ console.log(arg.name); }else{ console.log("没有这个名字"); } } show(obj3); show(obj1);
//关于数组 //创建数组和存取数组 var arr1=new Array(); arr1=[1,2,3]; var arr2=[]; var arr3=["吴琼",2,"str",{name:"吴琼",age:28}]; arr2[0]=1; arr1[3]=4; arr1[arr1.length]=5; console.log(arr1); console.log(arr1[arr1.length-1]);//最后一项的索引始终是length-1 console.log(arr2); console.log(arr2[0]); console.log(arr3); console.log(arr3[3].name); if(arr1 instanceof Array){ console.log("arr1 是数组"); }else{ console.log("arr1 不是数组"); } if(Array.isArray(arr2)){ console.log("arr2 是数组"); }else{ console.log("arr2 不是数组"); }; console.log(arr1.toString()); console.log(arr1.toLocaleString()); console.log(arr1.valueOf()); console.log(arr1.join("/")); //alert(arr1.join()); //后台自动调用arr1.toString(); console.log(arr1.push(6,7,"8","pop()这个项"));//返回新数组的长度 console.log(arr1); console.log( arr1.pop()); //返回取出来的项 console.log(arr1); var arr4=new Array(); console.log(arr4.unshift(1,2)); //返回新数组的长度 2 console.log(arr4); console.log(arr4.shift()); console.log(arr4); console.log(typeof arr4.shift()); console.log(arr4); var arr5=[0,5,15,10,20,"a","b",30]; console.log(arr5.reverse()); //改变了原数组 console.log(arr5.sort()); //[0,10,15,20,5] 转变为字符串比较后的排序结果 console.log(arr5.sort(function(a,b){return a>b})); var colors=[1,2,3]; var colors2=colors.concat(4,[5,6]); console.log(colors2); //返回一个新数组 var colors3=colors2.slice(1); var colors4=colors2.slice(1,5); var colors5=colors2.slice(1,colors2.length-1); console.log(colors3); console.log(colors4); console.log(colors5); var cars =["奔驰","宝马","奥迪"]; var cars1 =cars.splice(0,1); //删除奔驰 console.log(cars); console.log(cars1); var cars2 = cars.splice(0,1,"奔驰","别克");//替换宝马 console.log(cars); console.log(cars2); var cars3 = cars.splice(2,0,"大众"); //插入大众 console.log(cars); console.log(cars3); var age={name:"wq"}; var age1=[{name:"wq"},2,{a:2,b:3}]; if(!Array.indexOf){ Array.prototype.indexOf = function(el){ for (var i=0,n=this.length; i<n; i++){ if (this[i] === el){ return i; } } return -1; } } console.log(age1.indexOf({name:"wq"})); //-1 console.log(age1.indexOf(2)); //1 console.log(arr1.indexOf({a:2,b:3}));//-1 var iter=[1,2,3,4,3,2,1]; var everyResult = iter.every(function(item,index,array){ return item > 2; }); console.log(everyResult); var someResult = iter.some(function(item,index,array){ return item > 2; }); console.log(someResult); var filterResult = iter.filter(function(item,index,array){ return item > 2; }); console.log(filterResult); var mapResult = iter.map(function(item,index,array){ return item > 2; }); console.log(mapResult); //这个返回一个由布尔值组成的数组 var mapResult1 = iter.map(function(item,index,array){ return item * 2; }); console.log(mapResult1); iter.forEach(function(item,index,array){ //这里执行一些代码 if(array[index]>2){ console.log(item); }else{ } }); console.log(iter); var sum=iter.reduce(function(pre,cur,index,array){ return pre + cur; }); console.log(sum);
JavaScript的进阶之路(三)引用类型之Object类型和Array类型
标签:如何 有用 reac type 语法 转换 支持 排序 blog
原文地址:http://www.cnblogs.com/wow1314/p/7559723.html