最近面試js部分試題總結

來源:互聯網
上載者:User

標籤:變數   arguments   維數   sha   整合   some   blog   asc   合數   

 二,JavaScript面試題總結

1,首先是數組去重演算法:給一個數組,去掉重複值

(function() {        var arr = [1, 2, 3, 3, 4, ];        function unique() {            var result = [];            var tem = {};            for (var i = 0; i < arr.length; i++) {                if (!tem[arr[i]]) {                    result.push(arr[i]);                    tem[arr[i]] = 1;                }            }            return result;        }    })();

 2,多維陣列,至少3層的遍曆,將數組整合一維數組,網上給出的方案

 //遍曆多維陣列 var arr = [1, 2, 3, [4, [5, [6]]]]; // arr.length                                          Array.prototype.each = function(fn) {    try {        //1 目的: 遍曆數組的每一項 //計數器 記錄當前遍曆的元素位置                                                    this.i || (this.i = 0); //var i = 0 ;                                                  //2 嚴謹的判斷什麼時候去走each核心方法                                                               // 當數組的長度大於0的時候 && 傳遞的參數必須為函數                                                         if (this.length > 0 && fn.constructor == Function) {            // 迴圈遍曆數組的每一項                                                                         while (this.i < this.length) { //while迴圈的範圍                                              //擷取數組的每一項                                                                            var e = this[this.i];                //如果當前元素擷取到了 並且當前元素是一個數組                                                              if (e && e.constructor == Array) {                    // 直接做遞迴操作                                                                            e.each(fn);                } else {                    //如果不是數組 (那就是一個單個元素)                                                                  // 這的目的就是為了把數組的當前元素傳遞給fn函數 並讓函數執行                                                     //fn.apply(e,[e]);                                                                    fn.call(e, e);                }                this.i++;            }            this.i = null; // 釋放記憶體 記憶體回收機制回收變數                                                }    } catch (ex) {        // do something                                                                   }    return this;}

 3,獲得url查詢參數方案

1)一個是用Regex方法

//擷取url參數function GetQueryString(name) {    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");    //js match返回的是一數組    var r = location.search.substr(1).match(reg);    if (r != null) {        return r[2];    } else {        return null;    }}

 2)另外一種就是利用split結合數組遍曆實現,這個比較容易實現就不貼代碼了

4,Regex去掉空格

/**去掉字串前後所有空格*/function trim(str){ return str.replace(/(^\s*)|(\s*$)/g, ""); } 

 5,閉包的概念考察

6,原型繼承如何?,原型繼承有兩種方案

1)第一種是利用prototype

 var obj={name:‘seven‘};      var a=function(){};      a.prototype=obj;      var  aa=new a();      alert(aa.name);

 2)第二種是利用apply或者call

function people(name,age){            //屬性            this.name=name;            this.age=age;            //方法            this.show=function(){                console.log("my name is"+this.name+" and I am "+this.age+" years old");            };        }        function student(name,age,school){            people.apply(this,arguments);            this.school=school;            this.showYourself=function(){                console.log("my name is"+this.name+" and I am "+this.age+" years old"+" my school is"+ this.school);            };        }        var tom=new student(‘tom‘,‘19‘,‘xtu‘);        tom.showYourself();

 7,ES6常用知識點考察

JavaScript部分就到這。

 

最近面試js部分試題總結

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.