<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        PrototypeFunction對象學習_prototype

        來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 20:43:28
        文檔

        PrototypeFunction對象學習_prototype

        PrototypeFunction對象學習_prototype:這個對象就是對function的一些擴充,最重要的當屬bind方法,prototype的幫助文檔上特意說了一句話:Prototype takes issue with only one aspect of functions: binding.其中wrap方法也很重要,在類繼承機制里面就是利用wrap方法來調(diào)用父類的同名方
        推薦度:
        導讀PrototypeFunction對象學習_prototype:這個對象就是對function的一些擴充,最重要的當屬bind方法,prototype的幫助文檔上特意說了一句話:Prototype takes issue with only one aspect of functions: binding.其中wrap方法也很重要,在類繼承機制里面就是利用wrap方法來調(diào)用父類的同名方

        這個對象就是對function的一些擴充,最重要的當屬bind方法,prototype的幫助文檔上特意說了一句話:Prototype takes issue with only one aspect of functions: binding.其中wrap方法也很重要,在類繼承機制里面就是利用wrap方法來調(diào)用父類的同名方法。
        argumentNames
        bind
        bindAsEventListener
        curry
        defer
        delay
        methodize
        wrap
        代碼如下:
        //通過Object對象的extend方法對Function的prototype進行擴展
        Object.extend(Function.prototype, (function() {
        var slice = Array.prototype.slice;
        //把args添加到array后面,并返回array,內(nèi)部方法
        function update(array, args) {
        var arrayLength = array.length, length = args.length;
        while (length--) array[arrayLength + length] = args[length];
        return array;
        }
        //基本和update方法一樣,但是不改變傳入?yún)?shù)array,返回一個新的array
        function merge(array, args) {
        array = slice.call(array, 0);
        return update(array, args);
        }
        //把函數(shù)的參數(shù)格式化成數(shù)組,并返回
        function argumentNames() {
        var names = this.toString().match(/^[\s\(]*function[^(]*\(([^)]*)\)/)[1]
        .replace(/\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g, '')
        .replace(/\s+/g, '').split(',');

        return names.length == 1 && !names[0] ? [] : names;
        }
        //把執(zhí)行函數(shù)的上下文綁定到context
        function bind(context) {
        if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
        var __method = this, args = slice.call(arguments, 1);
        return function() {
        var a = merge(args, arguments);
        return __method.apply(context, a);
        }
        }
        //基本和bind差不多,就是保證傳入的第一個參數(shù)一定是event對象
        function bindAsEventListener(context) {
        var __method = this, args = slice.call(arguments, 1);
        return function(event) {
        var a = update([event || window.event], args);
        return __method.apply(context, a);
        }
        }
        //curry是一個數(shù)學家的名字,這個方法的作用就是可以連續(xù)的傳入?yún)?shù),看下面的具體例子就知道了
        function curry() {
        if (!arguments.length) return this;
        var __method = this, args = slice.call(arguments, 0);
        return function() {
        var a = merge(args, arguments);
        return __method.apply(this, a);
        }
        }
        //window.setTimeout函數(shù)的簡單封裝
        function delay(timeout) {
        var __method = this, args = slice.call(arguments, 1);
        timeout = timeout * 1000
        return window.setTimeout(function() {
        return __method.apply(__method, args);
        }, timeout);
        }
        //相當于delay(0.01)
        function defer() {
        var args = update([0.01], arguments);
        return this.delay.apply(this, args);
        }
        //用wrapper包裝將要調(diào)用的函數(shù),實現(xiàn)了簡單的AOP功能
        function wrap(wrapper) {
        var __method = this;
        return function() {
        var a = update([__method.bind(this)], arguments);
        return wrapper.apply(this, a);
        }
        }
        //把當前上下文作為第一個參數(shù)顯示的傳入調(diào)用的方法
        function methodize() {
        if (this._methodized) return this._methodized;
        var __method = this;
        return this._methodized = function() {
        var a = update([this], arguments);
        return __method.apply(null, a);
        };
        }
        //返回外部可調(diào)用的函數(shù)
        return {
        argumentNames: argumentNames,
        bind: bind,
        bindAsEventListener: bindAsEventListener,
        curry: curry,
        delay: delay,
        defer: defer,
        wrap: wrap,
        methodize: methodize
        }
        })());

        update,merge方法:由于是內(nèi)部方法,就不詳細說了,看源代碼基本上能看懂
        argumentNames方法:
        基本就是利用正則表達式提出方法里面的參數(shù)列表,并且刪除空格和一些特殊字符,然后用','進行分割,最后返回參數(shù)數(shù)組,我不明白最后返回 names.length == 1 這個條件有什么用?我試了試,去了也沒什么影響,知道的告訴我一下。下面看一下示例:
        代碼如下:
        var fn = function(foo, bar) { return foo + bar; };
        fn.argumentNames(); //-> ['foo', 'bar']
        Prototype.emptyFunction.argumentNames(); //-> []

        bind方法:
        首先判斷傳進來的參數(shù)個數(shù),至少要傳進來一個context參數(shù),如果直接調(diào)用bind()方法,那么將返回原來的函數(shù)對象。就相當于沒調(diào)用一樣。
        bind方法的原型是這樣的:bind(thisObj[, arg...]) -> Function,第一個參數(shù)后面可以跟可選的參數(shù),在bind方法里面用args變量存儲了除了第一個參數(shù)之外的所有其它參數(shù):args = slice.call(arguments, 1);
        var __method = this,這句話的意思是把__method變量設為當前的函數(shù),通過例子說明更清楚些:
        代碼如下:
        var obj = {
        name: 'A nice demo',
        fx: function() { alert(this.name); }
        };
        window.name = 'I am such a beautiful window!';
        function runFx(f) { f(); }
        //其中__method就相當于obj.fx
        var fx2 = obj.fx.bind(obj);
        runFx(obj.fx); //I am such a beautiful window!
        runFx(fx2); //A nice demo
        /*
        這里如果我們不在runFx函數(shù)里面調(diào)用f(),而是直接在外面調(diào)用obj.fx()那么得到的結果將是'A nice demo'。
        其實如果我們這樣寫:var f=obj.fx;f();那也將得到‘I am such a beautiful window!'。
        通過上面的例子,我們應該能看出上下文的概念:
        obj.fx(); //上下文為:obj
        f(); //上下文為:window
        可以看出上下文其實就是最后一個'.'之前的那個對象,如果直接調(diào)用函數(shù)則上下文為window
        */

        最后返回一個應用于context上下文的匿名函數(shù)。
        注意:var a = merge(args, arguments);這句話里面的arguments和args = slice.call(arguments, 1);里面的arguments是不一樣的。看一下例子:
        代碼如下:
        var obj = {
        name: 'A nice demo',
        fx: function() {
        alert(this.name + '\n' + $A(arguments).joi(', '));
        }
        };
        //這里的[1,2,3]就是slice.call(arguments, 1);里面的arguments
        var fx2 = obj.fx.bind(obj, 1, 2, 3);
        //這里的[4,5]就是merge(args, arguments);里面的arguments
        fx2(4, 5);
        // Alerts the proper name, then "1, 2, 3, 4, 5"

        bindAsEventListener方法:
        這個方法和bind差不多,最主要差別在這句:var a = update([event || window.event], args);總是保證綁定的函數(shù)第一個參數(shù)為event對象。看一下示例:
        代碼如下:
        var obj = { name: 'A nice demo' };
        function handler(e) {
        var data = $A(arguments);
        data.shift();
        alert(this.name + '\nOther args: ' + data.join(', ')); }
        handler.bindAsEventListener(obj, 1, 2, 3);
        //=======================


        curry方法:
        這個方法的個人覺得幫助文檔上給的例子不好,下面給出另一個示例,一看就明白了:
        代碼如下:
        var F=function(){alert(Array.prototype.slice.call(arguments,0).join(' '))};
        F.curry('I').curry('am').curry('never-online').curry('http://www.never-online.net')();
        //I am never-online http://www.never-online.net

        delay和defer方法:
        基本就是window.setTimeout的簡單封裝,時間單位為秒,看一下示例:
        代碼如下:
        // clearing a timeout
        var id = Element.hide.delay(5, 'foo');
        window.clearTimeout(id);

        wrap方法:
        Returns a function “wrapped” around the original function.
        Function#wrap distills the essence of aspect-oriented programming into a single method, letting you easily build on existing functions by specifying before and after behavior, transforming the return value, or even preventing the original function from being called.
        這句話:var a = update([__method.bind(this)], arguments);的意思就是把被包裝的函數(shù)當作第一個參數(shù)傳入包裝函數(shù),看一下示例:
        代碼如下:
        function wrapped(){
        alert('wrapped');
        }
        //可以在wrapper之前調(diào)用原函數(shù)或者之后調(diào)用,是不是有點AOP的意思了
        var wrapper=wrapped.wrap(function(oldFunc,param){
        //oldFunc()
        alert(param);
        oldFunc();
        });

        //wrapper,wrapped
        wrapper("wrapper");

        methodize方法:
        Takes a function and wraps it in another function that, at call time,
        pushes this to the original function as the first argument.
        這個方法先檢查將要被methodize的方法是否已經(jīng)methodize過了,通過內(nèi)部的變量this._methodized做檢查,
        最后methodize函數(shù)返回的其實就是this._methodized。
        這句話:var a = update([this], arguments);是關鍵,可以看出把this當成第一個參數(shù)傳到這個原始函數(shù)中了。看一下示例就明白了:
        代碼如下:
        // start off with a simple function that does an operation
        // on the target object:
        var fn = function(target, foo) { target.value = foo; }; var object = {};
        // 原始的方法
        fn(object, 'bar');
        object.value //-> 'bar'
        //調(diào)用methodize之后,可以看出fn函數(shù)第一個參數(shù)target變成了object
        object.fnMethodized = fn.methodize();
        object.fnMethodized('boom!');
        object.value //-> 'boom!'

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        PrototypeFunction對象學習_prototype

        PrototypeFunction對象學習_prototype:這個對象就是對function的一些擴充,最重要的當屬bind方法,prototype的幫助文檔上特意說了一句話:Prototype takes issue with only one aspect of functions: binding.其中wrap方法也很重要,在類繼承機制里面就是利用wrap方法來調(diào)用父類的同名方
        推薦度:
        標簽: 學習 fun 對象
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 88av免费观看入口在线| 精品一区二区三区免费毛片爱 | 国产精品亚洲片在线花蝴蝶| 大学生一级毛片免费看| 亚洲日本国产综合高清| 我要看WWW免费看插插视频| 亚洲精品美女网站| 免费看香港一级毛片| 久久久久亚洲国产AV麻豆 | 国产成人精品亚洲| 亚洲国产精品13p| 丝袜足液精子免费视频| 亚洲小视频在线观看| 国产成人精品免费视频动漫 | 黄色毛片免费网站| 久久99亚洲综合精品首页| 国产免费一区二区三区不卡| 久久亚洲精品成人AV| 成年女人毛片免费视频| 无人视频在线观看免费播放影院| 在线精品亚洲一区二区三区| 久久这里只精品99re免费| 涩涩色中文综合亚洲| 亚洲av成人一区二区三区在线观看| 一个人免费观看www视频| 亚洲精品高清久久| 日韩精品视频免费网址| 国产免费区在线观看十分钟| 久久夜色精品国产嚕嚕亚洲av| 国产大片91精品免费观看不卡| 亚洲欧洲无卡二区视頻| 亚洲熟妇少妇任你躁在线观看无码 | 极品美女一级毛片免费| 亚洲精品在线观看视频| 免费无码又爽又刺激高潮的视频| aaa毛片免费观看| 亚洲综合丁香婷婷六月香| 亚洲精品老司机在线观看| jjizz全部免费看片| 男性gay黄免费网站| 亚洲黄网站wwwwww|