仿寫數(shù)組對象


title: 仿寫數(shù)組對象
date: 2017-11-13 20:26:25
tags:


數(shù)組對象 <font style="color: #8D0000;">push | pop | shift | unshift | reverse | splice | join | slice</font>

prototype給某個類添加屬性或方法

    var a=[1,2,3];
    var b=[4,5,6];

<font style="color: #8000D0;">給數(shù)組類(所有的數(shù)組對象)添加myPush方法</font>

        Array.prototype.myPush=function(){
            // 哪個數(shù)組對象調(diào)用myPush,this就指向調(diào)用myPush的數(shù)組對象
            // console.log(this);
            // 調(diào)用的時候,寫了多少個參數(shù),arguments就有幾個元素,arguments是所有參數(shù)組成的數(shù)組,稱為參數(shù)列表
            // console.log(arguments);
            // arguments參數(shù)列表,獲取用戶寫入的參數(shù)列表
            for(var i=0;i<arguments.length;i++){
                this[this.length]=arguments[i];
            }
        }
        a.myPush(44,55,66);
        console.log(a);

<font style="color: #8000D0;">給數(shù)組類(所有的數(shù)組對象)添加myPop方法</font>

        Array.prototype.myPop=function(num){
            // 無參數(shù)
            // var res=this[this.length-1];
            // this.length=this.length-1;
            // 有參數(shù)
            var res=[];
            for(var i=this.length-num;i<this.length;i++){
                res.myPush(this[i]);
            }

            this.length=this.length-num;
            return res;
        }
        // a.myPop(2);
        console.log(a.myPop(2),a);

<font style="color: #8000D0;">給數(shù)組類(所有的數(shù)組對象)添加myReverse方法</font>

        Array.prototype.myReverse=function(){
            for(var i=0;i<parseInt(this.length/2);i++){
                // this[i]和this[this.length-1-i]交換即可
                var temp=this[i];
                this[i]=this[this.length-1-i];
                this[this.length-1-i]=temp;
            }
        }
        console.log(a);
        a.myReverse();
        console.log(a);

<font style="color: #8000D0;">給數(shù)組類(所有的數(shù)組對象)添加myUnshift方法</font>

        Array.prototype.myUnshift=function(){
            this.myReverse();
            for(var i=arguments.length-1;i>=0;i--){
                this.myPush(arguments[i]);
            }
            this.myReverse();
        }
        console.log(a);
        a.myUnshift(100,200);
        console.log(a);

<font style="color: #8000D0;">給數(shù)組類(所有的數(shù)組對象)添加myShift方法</font>

        Array.prototype.myShift=function(){
            this.myReverse();
            var res=this.myPop(2);
            this.myReverse();
            return res;
        }
        // a.myShift();
        console.log(a.myShift(),a);

<font style="color: #8000D0;">給數(shù)組類(所有的數(shù)組對象)添加myJoin方法</font>

        Array.prototype.myJoin=function(str){
            if(str===undefined){
                str=",";
            }
            var all='';
            for(var i=0;i<this.length-1;i++){
                all += this[i]+str;
            }
            all+=this[this.length-1];
            return all;

            // // 方法二
            // all+=this[0];
            // for(var i=1;i<this.length-1;i++){
            //  all += this[i]+str;
            // }
            // return all;

            // // 方法三
            // var all='';
            // for(var i=0;i<this.length-1;i++){
            //  if(i==this.length-1){
            //      str="";
            //  }
            //  all += this[i]+str;
            // }
            // return all;
        }
         console.log(a.myJoin(""));

         console.log(Boolean("")===Boolean(undefined));

<font style="color: #8000D0;">給數(shù)組類(所有的數(shù)組對象)添加mySplice方法</font>

        Array.prototype.mySplice=function(index,length){
            var res=[];
            var newArr=[];
            // 把index-length范圍的數(shù)做成數(shù)組,作為mySplice的返回值
            for(var i=index;i<index+length;i++){
                res.myPush(this[i]);
            }
            // 拼成新的數(shù)組分為三部分
            // 1、范圍之前,index之前
            for(var i=0;i<index;i++){
                newArr.myPush(this[i]);
            }
            // 2、參數(shù)列表里面下標2(包括下標2)之后的參數(shù)
            for(var i=2;i<arguments.length;i++){
                newArr.myPush(arguments[i]);
            }
            // 3、范圍之后,index+length開始,到最后一個
            for(var i=index+length;i<this.length;i++){
                newArr.myPush(this[i]);
            }
            // newArr 新數(shù)組
            this.length=0;
            for(var i=0;i<newArr.length;i++){
                this.myPush(newArr[i]);
            }
            return res;
        }
        console.log(a);
        console.log(a.mySplice(2,1,33,44,55,66));
        console.log(a);

<font style="color: #8000D0;">給數(shù)組類(所有的數(shù)組對象)添加mySlice方法</font>

        Array.prototype.mySlice=function(start,end){
            var res=[];
            for(var i=start;i<end;i++){
                res.myPush(this[i]);
            }
            return res;
        }
        console.log(a);
        console.log(a.mySlice(2,4));
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學一百閱讀 3,679評論 0 4
  • title: 仿寫字符串對象date: 2017-11-14 22:02:18tags: 字符串類型 <font ...
    淺夏_cd06閱讀 170評論 0 0
  • 多線程-thread Thread類和Runnable接口都在java.lang包中。 內(nèi)容: 1.多線程的基本概...
    可樂W閱讀 490評論 0 0
  • 原型和原型鏈 示例如何準確判斷一個變量是數(shù)組類型寫一個原型鏈繼承的例子描述 new 一個對象的過程zepto(或其...
    xunuo0x閱讀 299評論 0 0
  • 一整天都在忙碌著。 早上起晚了,安頓好小孩后猛然記起昨晚念及幾遍今早要買上佳肥豬肉。急急忙忙,拖著倆孩子的手,去肉...
    添一抹嵐閱讀 643評論 0 5

友情鏈接更多精彩內(nèi)容