bind 以及原型 1px邊框的實現(面試後內容整理)

一、bind函數返回的是一個function 函數
因此對bind函數擴展能夠是:

 Function.prototype.mybind = function mybind (context) {
       var _this = this;
       var outArg = Array.prototype.slice.call(arguments,1);
       // 兼容狀況下
       if('bind' in Function.prototype) {
           return this.bind.apply(this,[context].concat(outArg));
       }
       // 不兼容狀況下
       return function () {
           var inArg = Array.prototype.slice.call(arguments,0);
           inArg.length === 0?inArg[inArg.length]=window.event:null;
           var arg = outArg.concat(inArg);
           _this.apply(context,arg);
       }
   }
    function bbb(){
       console.log(this.aaa);
    }
   bbb.bind(o)()

 

 

二、理解原型,構造函數,實例css

以下圖:git

 

 

Range函數:github

   
//構造函數
function Range(to,from){ this.from=from; this.to=to; console.log(this
); }
// Range的原型 Range.prototype
={ include:function(x){return this.from<=x&&x<=this.to}, foreach:function(f){ for (var x=Math.ceil(this.from);x<=this.to;x++){ f(x); } }, toString:function(){ return "("+this.from+"..."+this.to+")"; } }
//實例
var b=new Range(1,3);

  

構造函數的this是 Object正則表達式

 

題目是這樣的有道筆試題;查找a在b字符串的位置,app

例如:'aaa'   'bbbaaa'  返回3函數

這道題仍是挺有意思當時我想到的是使用正則表達式;經過match來的到 結果取到index就能夠了;可是注意還要考慮正則的特殊字符的特殊處理; this

 

 

1px邊框的實現:spa

https://jinlong.github.io/2015/05/24/css-retina-hairlines/   (mark)prototype

相關文章
相關標籤/搜索