面試經驗分享

分享一些面試遇到的問題
面試分享


原文鏈接

1、同源策略:

同源策略:瀏覽器安全策略,同協議、ip、端口的腳本纔會執行;
跨域同源策略解決:帶src屬性的標籤可以跨域。jsonp(json的一種使用模式):向服務端拋出一個callback方法,數據以參數傳入,再有前端執行callback獲得數據。

2、閉包:

for(var i=0;i<10;i++){
   setTimeout(function(){
       console.log(i);
   },50);
}

輸出都是10;
改進:

for(var i=0;i<10;i++){
    (function(i){
        setTimeout(function(){
        console.log(i);
        },50);
    })(i);
}

3、對象深拷貝:

var a= { “child1” : 1, “child2” : { “key” : “value” } }
var b= copy(a);

實現:

function copy (obj) {
    var newobj = {};
    for (var i in obj) {
        if (typeof obj[i] == "object") {
            newobj[i] = copy(obj[i]);
        } else {
            newobj[i] = obj[i];
        }
     }
     return newobj;
}

4、數組去重:實現:

Array.prototype.unique = function() {
  var newArray = [];
  var obj = {};
  for (var i = 0; i < this.length; i++) {
     if (!obj[this[i]]) {
         newArray.push(this[i]);
         obj[this[i]] = 1;
     }
  }
  return newArray;
}

5、窗口變化調整方法優化:

function resizehandler() {
   console.log('exe');
}
function throttle(method, context) {
   clearTimeout(method.tId);
   method.tId = setTimeout(function() {
      method.call(context);
   }, 200);
}
window.onresize = function() {
   throttle(resizehandler, window);
}

6、css垂直居中顯示:

第一種實現:
#parent { position: relative }
#child {
position: absolute;
width: 30%;
height: 20%;
top: 50%;
left: 50%;
margin: -10% 0 0 -15%;
}
第二種實現:
#parent { position: relative }
#child {
position: absolute;
width: 30%;
height: 20%;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto
}

發佈了36 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章