复习(函数和数组)

  1. 自执行函数前后要加分号;(function(){})();
  2. 面向对象的最大优势节省了许多内存
    1. 复习函数

    函数是由关键字function声明的,他是一个引用数据类型,是Function这个类的实例,

    在调用的时候会开辟一个私有空间。

    1. 函数成员

    function fn(){} dir(fn)

    Arguments 是实参构成的数组集合

    Caller 是这个函数的调用者  函数在A函数内调用,那么A就是这个函数的调用者。

    在window下caller是null

    Length 是形参的个数

    Name就是函数名  只跟定义有关,不能修改

    2-1.arguments 函数的实参构成的数组集合 callee是当前函数本身

    递归求1-10的和

    1.
    function add(m,n){
    var a=m+n;
    if(n+1>10){
    return a
    }else{
    return add(a,n+1)
    }
    }
    console.log(add(1,2))

    2.
    var sum=0;
    function add(m){
    sum=sum+m;
    if(m<1){
    return sum;

    }else{
    return add(m-1)
    }
    }
    var as=add(10)
    console.log(as)

    3.
    var sum=0;
    function add(m){
    sum=sum+m;
    if(m<1){
    return sum;

    }else{
    return arguments.callee(m-1);
    }
    }
    var as=add(10)
    console.log(as)

    1. 函数作为参数

    1.作为匿名函数传

    2.作为命名函数传 传命名函数的函数名 不能带() 否则传的是return 返回值

  3.  

    1.

  4. function fn(f){
    console.log(111)
    f()
    console.log(333)
    }
    function ass(){
    console.log(222)
    }
    fn(ass)

    2.
    fn(function(){
    console.log(222)
    })

    1. 数组方法

    Sort(function(){})

    arr.sort(function(a,b){return a-b}) 只能对数字进行排序

    var arr=[45,56,23,11]
    arr.sort(function(a,b){return a-b})
    console.log(arr)
    var arr=["ehoijprs","ajrekg","uarioe","porbghkajk"]

    var arr=["1999-2-1","1998-1-2","2008-5-6"]
    function getSort(a,b){
    if(a>b){
    return 1;
    }else if(a==b){
    return 0;
    }else{
    return -1;
    }
    }
    arr.sort(getSort)
    console.log(arr)

  5.  

    数组的方法:Concat push sort toString splice unshift slice shift reverse pop join indexof

    Every() 用于检测数组中的每个元素是否都满足某个条件,返回值是true和false

    var arr=[25,56,78,26,56];
    var as=arr.every(function(value){
    return value>24
    })
    console.log(as)

    Filter() 用于检测数组中的每个元素是否都满足某个条件,返回的是满足条件的所有元素构造的新数组。

  6. var arr=[25,56,78,26,56];

    var a=arr.filter(function(value){
    return value>26
    })
    console.log(a)

    Find()用于检测数组中的第一个满足条件的元素并返回。

    var a=arr.find(function(value){
    return value>26
    })
    console.log(a)

    forEach()用于对数组进行遍历的

    arr.forEach(function(value){
    console.log(value)
    })

    Map() 用于对数组进行遍历,将每个元素都传入回调函数中加工,并返回新的结果,最后得到的是元素加工后组成的新的数组

    var a=arr.map(function(value){
    return value+10
    })
    console.log(a)

    1. 跟数据类型有关的一些方法

    (1)      检测数据类型  typeof  这是一个检测方法

    (2)      Instanceof  这是一个判断方法

    (3)      Object.prototype.toString() 以字符串[object Object]的形式告诉这种数据类型

    以字符串[object Array]的形式告诉是具体哪一种对象数据类型var arr=["ehoijprs","ajrekg","uarioe","porbghkajk"]
    var dates=new Date()
    console.log(typeof dates)
    console.log(Object.prototype.toString.call(dates))
    console.log(Object.prototype.toString.call(arr))

posted @ 2019-01-14 16:11  前端开创者123  阅读(138)  评论(0编辑  收藏  举报