zoukankan      html  css  js  c++  java
  • 六道JavaScript测验题

    1、找出数字数组中最大的元素(使用Match.max函数)

    1 var a=[123,23432,345,3,34];
    2 console.log(Math.max.apply(null,a));

    2、转化一个数字数组为function数组(每个function都弹出相应的数字)

    1 var a=[123,23432,345,3,34];
    2 a=a.map(function(value){
    3     return function(){
    4         alert(value);
    5     };
    6 });
    7 console.log(a[3]());

    3、给object数组进行排序(排序条件是每个元素对象的属性个数)

    var arr=[
        {a:1,b:2,c:3},
        {a:1,b:2,c:3,d:4,e:5},
        {a:1,b:2},
        {a:1,b:2,c:3,d:4}
    ];
    Object.prototype.propLength=(
        Object.prototype.hasOwnProperty('_count_')?
        function (){
            return this._count_; //FF
        }
        :
        function(){
        var p,count=0;
        for(p in this){
            if(this.hasOwnProperty(p)){
                count++;
            }
        }
        return count;
    }
    );
    function compare(obj1,obj2){
        return obj1.propLength()-obj2.propLength();
    }
    console.log(arr.sort(compare));

    4、利用JavaScript打印出Fibonacci数(不使用全局变量)

    1 function fibo(n){
    2   var self=arguments.callee;
    3   return n < 2 ? n : self(n-1)+self(n-2);
    4 }
    5 console.log(fibo(6));

    5、实现如下语法的功能:var a = (5).plus(3).minus(6); //2

    1 Number.prototype.plus=function (a){
    2     return this.valueOf()+a;
    3 };
    4 Number.prototype.minus=function (a){
    5     return this.valueOf()-a;
    6 };
    7 var a = (5).plus(3).minus(6);
    8 console.log(a);

    6、实现如下语法的功能:var a = add(2)(3)(4); //9

     1     function add(x){            
     2         var sum=x;
     3         var fn=function(y){
     4             sum+=y;
     5             return fn;
     6         };
     7         fn.valueOf=fn.toString=function(){return sum;};
     8         return fn;
     9     }
    10     add(2)(4);
    11     console.log(add(2));
    12     console.log(add(3)(8)(9));
    13     console.log(add(3)(8)(9)(10));

    欢迎批评指正。

  • 相关阅读:
    shell备份数据库
    inux系统设置只让一个固定的ip通过ssh登录和限制连接数量
    linux服务器配置可以执行java的jar包
    sql 查询多久之前的数据
    shell将sql查询结果存放到excel中
    shell编程从初学到精通
    Redis设置键的过期时间
    Java使用redis存取集合对象
    Jpa 连接数据库自动生成实体类
    Idea 开启Run Dashboard
  • 原文地址:https://www.cnblogs.com/yanyd/p/4202583.html
Copyright © 2011-2022 走看看