亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

前端攻城獅學習筆記二:實現一個叫Man的類,包

系統 2190 0

面試題目

  這是 搜狐JavaScript面試題 ,要求如下:

  實現一個叫Man的類,包含attr, words, say三個方法。

      
        var
      
      
         Man;


      
      
        //
      
      
        +++++++++++答題區域+++++++++++
      
      
        //
      
      
        +++++++++++答題結束+++++++++++
      
      
        try
      
      
        {

        

        
      
      
        var
      
       me = Man({ fullname: "小紅"
      
         });

        
      
      
        var
      
       she = 
      
        new
      
       Man({ fullname: "小紅"
      
         });

        

        console.group();

        console.info(
      
      "我的名字是:" + me.attr("fullname") + "\n我的性別是:" + me.attr("gender"
      
        ));

        console.groupEnd();



        
      
      
        /*
      
      
        ------[執行結果]------

        我的名字是:小紅

        我的性別是:<用戶未輸入>

        ------------------
      
      
        */
      
      
        



        me.attr(
      
      "fullname", "小明"
      
        );

        me.attr(
      
      "gender", "男"
      
        );

        me.fullname 
      
      = "廢柴"
      
        ;

        me.gender 
      
      = "人妖"
      
        ; 

        she.attr(
      
      "gender", "女"
      
        );

        

        console.group();

        console.info(
      
      "我的名字是:" + me.attr("fullname") + "\n我的性別是:" + me.attr("gender"
      
        ));

        console.groupEnd();



        
      
      
        /*
      
      
        ------[執行結果]------

        我的名字是:小明

        我的性別是:男

        ------------------
      
      
        */
      
      
        

        

        console.group();

        console.info(
      
      "我的名字是:" + she.attr("fullname") + "\n我的性別是:" + she.attr("gender"
      
        ));

        console.groupEnd();



        
      
      
        /*
      
      
        ------[執行結果]------

        我的名字是:小紅

        我的性別是:女

        ------------------
      
      
        */
      
      
        



        me.attr({

                
      
      "words-limit": 3
      
        ,

                
      
      "words-emote": "微笑"
      
        

        });

        me.words(
      
      "我喜歡看視頻。"
      
        );

        me.words(
      
      "我們的辦公室太漂亮了。"
      
        );

        me.words(
      
      "視頻里美女真多!"
      
        );

        me.words(
      
      "我平時都看優酷!"
      
        );

        

        console.group();

        console.log(me.say());



        
      
      
        /*
      
      
        ------[執行結果]------

        小明微笑:"我喜歡看視頻。我們的辦公室太漂亮了。視頻里美女真多!"

        ------------------
      
      
        */
      
      
        



        me.attr({

                
      
      "words-limit": 2
      
        ,

                
      
      "words-emote": "喊"
      
        

        });



        console.log(me.say());

        console.groupEnd();



        
      
      
        /*
      
      
        ------[執行結果]------

        小明喊:"我喜歡看視頻。我們的辦公室太漂亮了。"

        ------------------
      
      
        */
      
      
        

        

}
      
      
        catch
      
      
        (e){

        console.error(
      
      "執行出錯,錯誤信息: " +
      
         e);

}
      
    

分析過程

  分析如下:

  從實例化對象的方式看,用new或不用都可以,這是一種作用域安全構造函數,原理就是檢查this是不是指向當前方法本身,如果不是就強制new一個對象出來。所以大體框架如下:

      Man=
      
        function
      
      
        (obj){

    
      
      
        if
      
      (
      
        this
      
      
        instanceof
      
      
         arguments.callee){

        
      
      
        for
      
      (
      
        var
      
       e 
      
        in
      
      
         obj){

            
      
      
        this
      
      [e]=
      
        obj[e];

        }

    }

    
      
      
        else
      
      
        {

        
      
      
        return
      
      
        new
      
      
         Man(obj);

    }

};
      
    

  通過觀察可以發現,attr方法可以獲取或設置屬性值,并且參數可以是一個字符串,一個字符串加一個值,一個對象。所以attr方法定義如下:

      Man.prototype.attr=
      
        function
      
      
        (attr,val){

    
      
      
        if
      
      
        (val){

        
      
      
        this
      
      [attr]=
      
        val;

    }

    
      
      
        else
      
      
        {

        
      
      
        if
      
      (
      
        typeof
      
       attr === "string"
      
        ){

            
      
      
        return
      
      
        this
      
      .hasOwnProperty(attr)?
      
        this
      
      [attr]:"<用戶未輸入>"
      
        ;

        }

        
      
      
        else
      
      
        if
      
      (
      
        typeof
      
       attr === "object"
      
        ){

            
      
      
        for
      
      (
      
        var
      
       e 
      
        in
      
      
         attr){

                
      
      
        this
      
      [e]=
      
        attr[e];

            }

        }

        
      
      
        else
      
      
        {

        }

    }

}
      
    

  通過觀察words可以發現,這里就是傳入了一些字符串,以便在say方法中調用,所以這里直接給Man添加一個words_limit_arr的數組,用來存放這些字符串,所以words方法實現如下:

      Man.prototype.words=
      
        function
      
      
        (str){

    
      
      
        this
      
      
        .words_limit_arr.push(str);

}
      
    

  最后就是say方法了,通過觀察可以發現,say方法就是把fullname屬性的值加上limit-emote的值,再加上前words-limit個words_limit_arr項連接成的字符串,所以定義如下:

      Man.prototype.say=
      
        function
      
      
        (){

    
      
      
        return
      
      
        this
      
      ["fullname"]+
      
        this
      
      ["words-emote"]+":"+"\""+
      
        this
      
      .words_limit_arr.slice(0,
      
        this
      
      ["words-limit"]).join("")+"\""
      
        ;

}
      
    

  整個合起來就是:

      
        var
      
       Man=
      
        function
      
      
        (obj){

    
      
      
        if
      
      (
      
        this
      
      
        instanceof
      
      
         arguments.callee){

        
      
      
        for
      
      (
      
        var
      
       e 
      
        in
      
      
         obj){

            
      
      
        this
      
      [e]=
      
        obj[e];

        }

        
      
      
        this
      
      .words_limit_arr=
      
        [];

    }

    
      
      
        else
      
      
        {

        
      
      
        return
      
      
        new
      
      
         Man(obj);

    }

};



Man.prototype.attr
      
      =
      
        function
      
      
        (attr,val){

    
      
      
        if
      
      
        (val){

        
      
      
        this
      
      [attr]=
      
        val;

    }

    
      
      
        else
      
      
        {

        
      
      
        if
      
      (
      
        typeof
      
       attr === "string"
      
        ){

            
      
      
        return
      
      
        this
      
      .hasOwnProperty(attr)?
      
        this
      
      [attr]:"<用戶未輸入>"
      
        ;

        }

        
      
      
        else
      
      
        if
      
      (
      
        typeof
      
       attr === "object"
      
        ){

            
      
      
        for
      
      (
      
        var
      
       e 
      
        in
      
      
         attr){

                
      
      
        this
      
      [e]=
      
        attr[e];

            }

        }

        
      
      
        else
      
      
        {

        }

    }

}



Man.prototype.words
      
      =
      
        function
      
      
        (str){

    
      
      
        this
      
      
        .words_limit_arr.push(str);

}



Man.prototype.say
      
      =
      
        function
      
      
        (){

    
      
      
        return
      
      
        this
      
      ["fullname"]+
      
        this
      
      ["words-emote"]+":"+"\""+
      
        this
      
      .words_limit_arr.slice(0,
      
        this
      
      ["words-limit"]).join("")+"\""
      
        ;

}
      
    

  但其中有個問題沒有解決,就是用me.fullname="xxx"方式的賦值不能改變me.attr("fullname","xxx")方式的賦值。這個問題導致運行效果如下:

前端攻城獅學習筆記二:實現一個叫Man的類,包含attr, words, say三個方法。

  這個問題要如何解決呢,我一直沒想到,后來參考了別人的代碼,那位朋友的思路是讓me.attr("fullname","xxx")方式對fullname的賦值跟me.fullname不是同一個屬性。

  在沒想到其他方案之前,我也只好把我的代碼修改如下:

      
        var
      
       Man=
      
        function
      
      
        (obj){

    
      
      
        if
      
      (
      
        this
      
      
        instanceof
      
      
         arguments.callee){

        
      
      
        this
      
      .infos=
      
        {
      
      
        };

        
      
      
        for
      
      (
      
        var
      
       e 
      
        in
      
      
         obj){

            
      
      
        this
      
      .infos[e]=
      
        obj[e];

        }

        
      
      
        this
      
      .words_limit_arr=
      
        [];

    }

    
      
      
        else
      
      
        {

        
      
      
        return
      
      
        new
      
      
         Man(obj);

    }

};



Man.prototype.attr
      
      =
      
        function
      
      
        (attr,val){

    
      
      
        if
      
      
        (val){

        
      
      
        this
      
      .infos[attr]=
      
        val;

    }

    
      
      
        else
      
      
        {

        
      
      
        if
      
      (
      
        typeof
      
       attr === "string"
      
        ){

            
      
      
        return
      
      
        this
      
      .infos[attr]?
      
        this
      
      .infos[attr]:"<用戶未輸入>"
      
        ;

        }

        
      
      
        else
      
      
        if
      
      (
      
        typeof
      
       attr === "object"
      
        ){

            
      
      
        for
      
      (
      
        var
      
       e 
      
        in
      
      
         attr){

                
      
      
        this
      
      .infos[e]=
      
        attr[e];

            }

        }

        
      
      
        else
      
      
        {

        }

    }

}



Man.prototype.words
      
      =
      
        function
      
      
        (str){

    
      
      
        this
      
      
        .words_limit_arr.push(str);

}



Man.prototype.say
      
      =
      
        function
      
      
        (){

    
      
      
        return
      
      
        this
      
      .infos["fullname"]+
      
        this
      
      .infos["words-emote"]+":"+"\""+
      
        this
      
      .words_limit_arr.slice(0,
      
        this
      
      .infos["words-limit"]).join("")+"\""
      
        ;

}
      
    

  修改后運行效果如下:

前端攻城獅學習筆記二:實現一個叫Man的類,包含attr, words, say三個方法。

小結

  本面試題主要考查了 作用域安全構造函數,instanceof/typeof 的用法,slice的用法,對數組的遍歷,對對象成員的遍歷等知識點。

  關于上面提到的“用me.fullname="xxx"方式的賦值不能改變me.attr("fullname","xxx")方式的賦值”問題,如果各位朋友有新的方案,請不吝指點。也歡迎對我的方案拍磚論討。

前端攻城獅學習筆記二:實現一個叫Man的類,包含attr, words, say三個方法。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久伊人免费视频 | 久久国产午夜精品理论片34页 | 一级毛片aa高清免费观看 | 欧美特级毛片a够爽 | 色噜噜狠狠狠狠色综合久一 | 香香在线观看视频 | 88国产精品视频一区二区三区 | 日日干日日射 | 久久爱www| 四虎国产精品免费五月天 | 中文字幕一区在线观看视频 | 妇女网站爱嘿嘿视频免费观看 | 亚洲欧洲日产国码久在线观看 | 国产成人经典三级在线观看 | 手机看片福利在线 | 午夜91| 成人a毛片一级 | 精品久久国产 | 深夜影院在线视频观看 | 国产精品线在线精品国语 | 亚欧乱色精品免费观看 | 亚洲精品一区henhen色 | 午夜手机看片 | 日韩精品一区二区三区中文精品 | 91国自产精品中文字幕亚洲 | 五月月色开心婷婷久久合 | 亚洲欧美日韩中文字幕在线一 | 裸身裸乳免费视频网站 | 国产a一级毛片午夜剧场14 | 99热久久国产精品免费看 | 欧美日韩在线看 | 久久婷五月综合 | 亚洲69av| 成人人观看的免费毛片 | 99精品网 | 性生活视频免费 | 欧美日日 | 亚洲国产二区三区久久 | 色哥网站| 2020久久精品永久免费 | 欧美成人性视频在线黑白配 |