Blogger Information
Blog 10
fans 1
comment 0
visits 9019
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
forEach/splice
枫的博客
Original
1270 people have browsed it

5月7号作业

//1、使用forEach遍历数组
/*当一个函数当另外一个函数参数的时候,这个函数叫做回调参数
forEach(function(当前元素,当前元素的索引,当前整个数组){})
既:forEach(function(value,key,array))
map和forEach很像,区别在与map是有返回值,forEach没有*/

<script>
var arr=['html','css','javascript'];
arr.forEach(function(value){console.log(value)}); //html,css,javascript
arr.forEach(function(value,key){console.log(key+':'+value)}); //0:html,1:css,2:javascript
arr.forEach(function(value,key,array){console.log(array)}); //数组被遍历3遍
arr.forEach(function(value,key,array){console.log(array[key])}); //html,css,javascript
arr.forEach(function(value,key,array){console.log(key+':'+array[key])});//0:html,1:css,2:javascript

//判断是不是数组
Array.isAarry(arr) //ture
arr instanceof object  //ture  //数组也是对象
typeof arr  //object

扩展-类数组对象
var arr1={0:'html', 1:'css' , 2:'javascript'};
Array.isAarry(arr1) //false
arr1 instanceof object  //ture
arr1 instanceof Array  //false
----------------------------------------------------------------------------------------------------
        
//2、splice()的添加,删除,更新
var arr2=[1,2,3,4,5,6,7,8];
arr2.splice(3)  // 返回[4,5,6,7,8]   
arr2.splice(2,2)  //返回[3,4],从第2个位置开始,删除2个,并返回删除的数组,注意:原数组已更改
arr2.splice(2,0,55,66) //返回空数组,从从第2个位置开始,插入55,66.既arr2=[1,2,55,66,5,6,7,8]
arr2.splice(2,2,3,4)//更新数组,返回[55,66],此时arr2=[1,2,3,4,5,6,7,8]
arr2.splice(-6,2)  //[3,4] 
arr2.splice(-2,0,55,66) //[] 此时arr2=[1,2,5,6,55,66,7,8]
arr2.splice(-2,2,77,88) //[7,8] 此时arr2=[1,2,5,6,55,66,77,88]

---------------------------------------------------------------------------------------------------
课后扩展
扩展slice() //切片,
一、从数组中提取部分成员
var arr3=[1,2,3,4,5,6,7];
arr3.slice()   //[1,2,3,4,5,6,7]
arr3.slice(2)  //[3, 4, 5, 6, 7]从第2个开始切除
arr3.slice(2,6)  //[3, 4, 5, 6]从第2个开始,第六个结束,但是第六个值不要
二、同时可把类数组对象转换成数组
var obj={0:'xiaomi';1:'huawei';2:'oppo';3:'iphone',length:3};
var arr4=Array.prototype.slice.call(obj) //arr4=["xiaomi", "huawei", "oppo"]  //将obj装换成数组  
arr4.forEach(function(value){console.log(value)}) //xiaomi huawei oppo //遍历数组

扩展原型和原型链
//对象字面量
var obj={a:1, b:2, c:function(){return this.a+this.b}}

//构造函数创建对象
function Foo(name,age){this.name=name;this.age=age}; //构造函数,做对象母版
var obj=new Foo('ping',100);      //通过构造函数,得到obj1对象
var obj1=new Foo('cat',100);     //通过构造函数,得到obj1对象
Foo.prototype.user=function(){return this.name+this.age} //设置Foo的原型
obj.user()    //ping100  //obj__proto__去Foo.prototype上找user叫做原型链
obj.user1=function(){return this.name+':'this.age}  //添加obj属性方法
obj.user1()  //ping:100 //obj=Foo {name: "ping", age: 100, user1: ƒ}


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post