17-自定义原型方法、去重、封装typeof

17-自定义原型方法、去重、封装typeof

(一)、自定义方法

myUnshift
(1)、用splice方法实现

用splice方法重写数组原型上的unshift方法 myUnshift

      var arr = ['d', 'e', 'f'];

      Array.prototype.myUnshift = function(){
         var pos = 0;
         for(var i = 0; i < arguments.length; i++){
            this.splice(pos,0,arguments[i]);
            pos ++;
         }
         return this.length;
      }
  
      arr.myUnshift('a',2,3);
      console.log(arr);

(2)、用concat方法实现

用concat方法重写数组原型上的unshift方法 myUnshift

// Array.prototype.slice.call()将类数组转换为数组

var arr = ['d', 'e', 'f'];

Array.prototype.myUnshift = function(){
	var argArr = Array.prototype.slice.call(arguments);
	var newArr = argArr.concat(this);
	return newArr;
}

var newArr = arr.myUnshift('a',2,3);
console.log(newArr);

(二)、去重

1、数组去重
借用对象属性名也就是key值的不重复性

这种写法会出现问题,因为第二次循环到0,temp[0] = 0,这时还会进入循环

      var arr = [0,0,1,1,1,2,2,2,3,3,3,'a','a'];

      Array.prototype.unique = function(){
         var temp = {},
             newArr = [];

         for(var i = 0; i < this.length; i++){
            if(!temp[this[i]]){           // temp[0] = 0  !0 -> true
               temp[this[i]] = this[i];
               newArr.push(this[i]);
            }
         } 

         return newArr;
      }

      console.log(arr.unique()); //[0,0,1,2,3,'a']

不要给0,随便给个字符串 temp[this[i]] = ‘value’; 就能解决这个问题

      var arr = [0,0,1,1,1,2,2,2,3,3,3,'a','a'];

      Array.prototype.unique = function(){
         var temp = {},
             newArr = [];

         for(var i = 0; i < this.length; i++){
            if(!temp[this[i]]){
               temp[this[i]] = 'value';
               newArr.push(this[i]);
            }
         } 
         return newArr;
      }

      console.log(arr.unique());

用hasOwnProperty这个方法也能解决上述问题

      var arr = [0,0,1,1,1,2,2,2,3,3,3,'a','a'];

      Array.prototype.unique = function(){
         var temp = {},
             newArr = [];

         for(var i = 0; i < this.length; i++){
            if(!temp.hasOwnProperty(this[i])){
               temp[this[i]] = this[i];
               newArr.push(this[i]);
            }
         } 
         return newArr;
      }

      console.log(arr.unique());
2、字符串去重
      var str = '111222000aabb';

      String.prototype.unique = function(){
         var temp = {},
             newStr = '';

         for(var i = 0; i < this.length; i++){
            if(!temp.hasOwnProperty(this[i])){
               temp[this[i]] = this[i];
               newStr += this[i];
            }
         } 
         return newStr;
      }

      console.log(str.unique());
笔试题:找出字符串中第一个不重复的字符(非常重要)

用计数的方式

      var str = 'bcabdd'

      function test(str){
         var temp = {};

         for(var i = 0; i < str.length; i++){
            if(temp.hasOwnProperty(str[i])){
               temp[str[i]]++;
            }else{
               temp[str[i]] = 1;
            }
         }
          
         for(var key in temp){
            if(temp[key] === 1){
               return key;
            }
         }
      }

      console.log(test(str)); //c

(三)、封装typeof

// typeof默认能返回 number string boolean object function undefined
//    //function其实也是引用值,但是typeof是可以单独返回function的
<script type="text/javascript">
    function myTypeof(val){
      var type = typeof(val);
      var toStr = Object.prototype.toString;
      var res = {
         '[object Array]': 'array',
         '[object Object]': 'object',
         '[object Number]': 'object number',
         '[object String]':'object string',
         '[object Boolean]':'object boolean'
      }

      if(val === null){
         return 'null';
      }else if(type === 'object'){
         var ret = toStr.call(val);
         return res[ret];
      }else{
         return type;
      }
    }
    console.log(myTypeof(1));             // number
    console.log(myTypeof('1'));           // string
    console.log(myTypeof(true));          // boolean
    console.log(myTypeof({}));            // object
    console.log(myTypeof([]));            // array
    console.log(myTypeof(new Number(1))); // object number
    console.log(myTypeof(new String(1))); // object string
    console.log(myTypeof(new Boolean(1))); // object boolean
    console.log(myTypeof(null));           // null
    console.log(myTypeof(undefined));      // undefined
    console.log(myTypeof(function(){}));   // function
</script>

一道闭包题
     function Test(a,b,c){

         var d = 0;
         this.a = a;
         this.b = b;
         this.c = c;

         function e(){
            d++;
            console.log(d);
         }

         this.f = e;

     }

     var test1 = new Test();
     test1.f();  //1
     test1.f();  //2
     var test2 = new Test();
     test2.f();  //1

去除掉迷惑性的东西

   function Test(a,b,c){

         var d = 0;      
         
         this.f = function(){
            d++;
            console.log(d);
         }
     }

     var test1 = new Test();
     test1.f();  //1
     test1.f();  //2
     var test2 = new Test();
     test2.f();  //1

题2:
function test(){
   console.log(typeof(arguments));
}
test(); //object
//arguments是类数组,用对象来模拟的数组
题3:
    var test = function a(){
       return 'a';
     }

     console.log(typeof(a)); //undefined

//函数表达式是忽略函数名a的
题4:

如何简化下面这个函数

      test(10);
    function test(day){
      switch(day){
         case 1:
            console.log('Mon');
            break;
         case 2:
            console.log('Tue');
            break;
         case 3:
            console.log('Wed');
            break;
         case 4:
            console.log('Thu');
            break;
         case 5:
            console.log('Fri');
            break;
         case 6:
            console.log('Sat');
            break;
         case 7:
            console.log('Sun');
            break;
         default:
            console.log('I don\'t know');      
                         
      }
    }

比switch更好的方法

 <script type="text/javascript">
     test(10);
     function test(day){
       var weekday = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];

       weekday[day - 1] !== undefined ? console.log(weekday[day-1])
                                      : console.log('I don\'t konw');

    }
 </script>
<!--不想用day-1,在数组前新增一个空的子项-->
 <script type="text/javascript">
     test(0);
    function test(day){
       var weekday = [,'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];

       weekday[day] !== undefined ? console.log(weekday[day])
                                      : console.log('I don\'t konw');

    }
 </script>
<!--利用数组的巧妙之处将位置为0的问题也解决了-->
   var arr = [, 1, 2, 3];
   console.log(arr[0]); //undefined

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值