<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關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題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關(guān)鍵字專題關(guān)鍵字專題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
        當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

        寫自已的js類庫需要的核心代碼_js面向?qū)ο?/h1>
        來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 21:03:41
        文檔

        寫自已的js類庫需要的核心代碼_js面向?qū)ο?/h4>
        寫自已的js類庫需要的核心代碼_js面向?qū)ο? 代碼如下: (function(win) { var toString = Object.prototype.toString; var hasOwn = Object.prototype.hasOwnProperty; var class2type = {}; class2type[[object Boolean]] = boolean; c
        推薦度:

        導(dǎo)讀寫自已的js類庫需要的核心代碼_js面向?qū)ο? 代碼如下: (function(win) { var toString = Object.prototype.toString; var hasOwn = Object.prototype.hasOwnProperty; var class2type = {}; class2type[[object Boolean]] = boolean; c

        代碼如下:
        (function(win) {
        var toString = Object.prototype.toString;
        var hasOwn = Object.prototype.hasOwnProperty;
        var class2type = {};
        class2type["[object Boolean]"] = "boolean";
        class2type["[object Number]"] = "number";
        class2type["[object String]"] = "string";
        class2type["[object Function]"] = "function";
        class2type["[object Array]"] = "array";
        class2type["[object Date]"] = "date";
        class2type["[object RegExp]"] = "regexp";
        class2type["[object Object]"] = "object";
        win.type = function(obj) {
        return obj == null ? String(obj) : class2type[toString.call(obj)] || "object";
        };
        win.isBoolean = function(obj) {
        return type(obj) === "boolean";
        };
        win.isNumber = function(obj) {
        return type(obj) === "number";
        };
        win.isString = function(obj) {
        return type(obj) === "string";
        };
        win.isDate = function(obj) {
        return type(obj) === "date";
        };
        win.isRegExp = function(obj) {
        return type(obj) === "regexp";
        };
        win.isObject = function(obj) {
        return type(obj) === 'object';
        };
        win.isFunction = function(obj) {
        return type(obj) === "function";
        };
        win.isArray = function(obj) {
        return type(obj) === "array";
        };
        win.isWindow = function(obj) {
        return obj
        && typeof obj === "object"
        && "setInterval" in obj;
        };
        win.isNumeric = function(obj) {
        return !isNaN(parseFloat(obj)) && isFinite(obj);
        };
        win.isPlainObject = function(obj) {
        if (!obj
        || type(obj) !== "object"
        || obj.nodeType
        || isWindow(obj)) {
        return false;
        }
        try {
        if (obj.constructor
        && !hasOwn.call(obj, "constructor")
        && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
        return false;
        }
        } catch (e) {
        return false;
        }
        var key;
        for (key in obj) {
        }
        return key === undefined || hasOwn.call(obj, key);
        };
        win.isEmptyObject = function(obj) {
        for ( var name in obj) {
        return false;
        }
        return true;
        };
        win.isPrimitive = function(obj){
        var type = typeof obj;
        return type === 'string' || type === 'number' || type === 'boolean';
        };
        //HTMLElement
        win.isElement = function(obj){
        return obj ? obj.nodeType === 1 : false;
        };
        //TextNode
        win.isTextNode = function(obj){
        return obj ? obj.nodeName === "#text" : false;
        };
        win.isIterable = function(obj){
        return (obj && typeof obj !== 'string') ? obj.length !== undefined : false;
        };
        win.isDefined = function(obj){
        return typeof obj !== 'undefined';
        };
        win.error = function(msg) {
        throw new Error(msg);
        };
        win.now = function() {
        return (new Date()).getTime();
        };
        win.print = function(value) {
        document.write(value);
        };
        win.println = function(value) {
        print(value);
        document.write("
        ");
        };
        win.each = function(object, callback, args) {
        var name, i = 0,
        length = object.length,
        isObj = (length === undefined || isFunction(object));
        if (args) {
        if (isObj) {
        for (name in object) {
        if (callback.apply(object[name], args) === false) {
        break;
        }
        }
        } else {
        for (; i < length;) {
        if (callback.apply(object[i++], args) === false) {
        break;
        }
        }
        }
        } else {
        if (isObj) {
        for (name in object) {
        if (callback.call(object[name], name, object[name]) === false) {
        break;
        }
        }
        } else {
        for (; i < length;) {
        if (callback.call(object[i], i, object[i++]) === false) {
        break;
        }
        }
        }
        }
        return object;
        };
        win.Array.prototype.toString = function(){
        return "[" + this.join() + "]"
        }
        win.extend = function() {
        var options,
        name,
        src,
        copy,
        copyIsArray,
        clone,
        target = arguments[0] || {},
        i = 1,
        length = arguments.length,
        deep = false;
        // Handle a deep copy situation
        if ( typeof target === "boolean" ) {
        deep = target;
        target = arguments[1] || {};
        // skip the boolean and the target
        i = 2;
        }
        // Handle case when target is a string or something (possible in deep copy)
        if ( typeof target !== "object" && !isFunction(target) ) {
        target = {};
        }
        // extend jQuery itself if only one argument is passed
        if ( length === i ) {
        target = this;
        --i;
        }
        for ( ; i < length; i++ ) {
        // Only deal with non-null/undefined values
        if ( (options = arguments[ i ]) != null ) {
        // Extend the base object
        for ( name in options ) {
        src = target[ name ];
        copy = options[ name ];
        // Prevent never-ending loop
        if ( target === copy ) {
        continue;
        }
        // Recurse if we're merging plain objects or arrays
        if ( deep && copy && ( isPlainObject(copy) || (copyIsArray = isArray(copy)) ) ) {
        if ( copyIsArray ) {
        copyIsArray = false;
        clone = src && isArray(src) ? src : [];
        } else {
        clone = src && isPlainObject(src) ? src : {};
        }
        // Never move original objects, clone them
        target[ name ] = extend( deep, clone, copy );
        // Don't bring in undefined values
        } else if ( copy !== undefined ) {
        target[ name ] = copy;
        }
        }
        }
        }
        // Return the modified object
        return target;
        };
        })(window);

        如果我們不用extend方法,可以象下面的方式寫自己的組件:
        代碼如下:
        (function(win){
        win.StringBuffer = function(){
        this.datas = [];
        }
        var proto = StringBuffer.prototype;
        proto.append = function(value){
        this.datas.push(value);
        },
        proto.toString = function(){
        return this.datas.join("");
        }
        })(window);

        如果使用extend方法,可以象下面這樣寫組件:
        代碼如下:
        (function(win){
        win.extend(win,{
        StringBuilder : function(){
        this.datas = [];
        }
        });
        win.extend(StringBuilder.prototype, {
        append : function(value){
        this.datas.push(value);
        },
        toString : function(){
        return this.datas.join("");
        }
        });
        })(window);

        兩種方法的效果一樣,但是extend方法還可以做更多事件,比如插件式開發(fā),跟第二種方式很象。
        當(dāng)然,如果你本來就想寫jQuery插件,那就直接用jQuery的extend就可以啦。

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

        文檔

        寫自已的js類庫需要的核心代碼_js面向?qū)ο?/h4>
        寫自已的js類庫需要的核心代碼_js面向?qū)ο? 代碼如下: (function(win) { var toString = Object.prototype.toString; var hasOwn = Object.prototype.hasOwnProperty; var class2type = {}; class2type[[object Boolean]] = boolean; c
        推薦度:

        標(biāo)簽: js 代碼 對(duì)象
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 一级特黄aaa大片免费看| 亚洲日本va在线观看| 国产精品内射视频免费| 可以免费观看的一级毛片| 久久水蜜桃亚洲AV无码精品| 在线观看免费人成视频色| 一本色道久久88—综合亚洲精品| 69堂人成无码免费视频果冻传媒| 亚洲成a人片77777群色| 在人线av无码免费高潮喷水| 亚洲中文字幕无码亚洲成A人片| 青青久在线视频免费观看| 亚洲精品精华液一区二区 | 日本精品久久久久久久久免费 | 亚洲毛片不卡av在线播放一区| 二级毛片免费观看全程| 亚洲中文字幕无码久久综合网| 久章草在线精品视频免费观看| 亚洲一级二级三级不卡| 久久久高清免费视频| 亚洲AV无码专区在线观看成人 | 国产精品免费视频一区| ww在线观视频免费观看w| 亚洲AV无码国产丝袜在线观看| www视频免费看| 亚洲av片在线观看| 国产亚洲精品成人a v小说| 久久精品乱子伦免费| 99久久婷婷国产综合亚洲| 国产三级免费电影| 无码AV片在线观看免费| 亚洲欧美日韩国产精品一区| 亚洲色欲色欲www在线丝 | 亚洲国产综合第一精品小说| 日韩免费电影在线观看| 99精品视频免费| 亚洲欧美日韩久久精品| 亚洲va无码va在线va天堂| 女人被弄到高潮的免费视频| 好男人资源在线WWW免费| 亚洲人成网国产最新在线|