js37---Function.prototype

來源:互聯網
上載者:User

標籤:dev   blog   val   變數   ddc   false   span   scribe   函數名   

    //給函數的prototype新增名字為name,函數體為fn的函數        Function.prototype.method =function(name,fn){        this.prototype[name] = fn;//this        return this;    }        //再擴充Array的方法,新方法的名字,函數體,形參,實參        if(!Array.prototype.forEach){        //Array.prototype.forEach = function(fn,thisObj){}                //this.fns.forEach(  function(el){el(o);}  ) function(el){el(o)是函數實參                Array.method("forEach",function(fn,thisObj){//forRach是函數名字,後面是函式宣告,fn,thisObj是聲明裡的形參            var scope = thisObj || window;            for (var i = 0; i < this.length; i++) {                fn.call(scope,this[i],i,this);            }        })    }    if(!Array.prototype.filter){        //this.fns.filter(function(el){if(el != xxx){return el;}})        Array.method("filter",function(fn,thisObj){            var scope = thisObj || window;            var a = [];            for (var i = 0; i < array.length; i++) {                if( !fn.call(scope,this[i],i,this) ){                    continue;                }                a.push(this[i])            }            //..........................            return a;        })    }
window.DED = window.DED||{};//有就用自己,沒有就用Null 物件    DED.util = DED.util || {}    //觀察者    DED.util.Observer = function(){        this.fns = []    }    //擴充他    DED.util.Observer.prototype = {        //觀察        subscribe : function(fn){            this.fns.push(fn);        },        //取消觀察        unsubscribe : function(fn){            this.fns = this.fns.filter(function(el){                if(el != fn){                    return el;                }            })        },        //迴圈執行函數被觀察的數組        fire:function(o){            this.fns.forEach(function(el){                el(o);            })        }    }
addEvent(items, ‘click‘, function(e) {          var e = e || window.event;          var src = e.target || e.srcElement;          try {            e.preventDefault();          }          catch (ex) {            e.returnValue = false;          }
function F(){}    var f = function(){}        Function.prototype.method =function(name,fn){        alert(1);    }    f.method();//1    F.method();//1 function F(){}    var f = function(){}        Function.prototype.method =function(name,fn){        this.prototype[name] = fn;        return this;    }    f.method("a",function(){alert("a");});//1    F.method("b",function(){alert("b");});//1    //f.a();//f.a is not a function    f.prototype.a();//a    //F.b();//F.b is not a function    F.prototype.b()//b    new F().b();//b

 形參看成建構函式傳入的成員變數的值。函數名看成類名。this.看成成員屬性和成員方法。

 

    function addCar(){        this.name = 11;        this.begin = function(){//對象的成員方法屬性可以用中括弧擷取            alert(1);        }    }    new addCar().begin();//1    alert(new addCar()["begin"]);//this.begin = function(){alert(1);}    alert(typeof new addCar()["begin"]);//function    new addCar()["begin"]();//1    alert(new addCar()["name"]);//11        var rr = new addCar();    rr["age"] = 444;    alert(rr["age"]);//444    rr["fff"] = function(){alert(777);}    rr["fff"]();//777

 

js37---Function.prototype

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.