博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js函数 eql,equal,equalp
阅读量:7111 次
发布时间:2019-06-28

本文共 5228 字,大约阅读时间需要 17 分钟。

function eql(obj, other) {        if(stringp(obj) && stringp(other) && obj === other) return false;        return obj === other;    }function equal(obj, other, equalp) {    if (equalp === void 0) { equalp = false; }    var _tostring = function (value) { return Object.prototype.toString.call(value); };    var emptyp = function (value) {        return JSON.stringify(value).length === 2 ? true : false;    };    function Equal(obj, other, equalp) {        var objTag = _tostring(obj);        var otherTag = _tostring(other);        var objectTag = '[object Object]';        var arrayTag = '[object Array]';        if (objTag !== objectTag && objTag !== arrayTag && otherTag !== objectTag && otherTag !== arrayTag) {            if (equalp && typeof obj === 'string' && typeof other === 'string') {                return (obj).toLocaleUpperCase() === (other).toLocaleUpperCase();            }            return obj === other;        }        if (objTag !== otherTag)            return false; // 集合类型不一样        if (Object.getOwnPropertyNames(obj).length !== Object.getOwnPropertyNames(other).length)            return false; // 集合元素数量不一样        if (emptyp(obj) && emptyp(other))            return true; // 类型一样的空集合,永远相等。        var data = (function () {            var data = Object.getOwnPropertyNames(obj);            if (objTag === arrayTag) {                data.pop();                return data;            }            else {                return data;            }        })();        for (var i in data) {            var k = data[i];            if (k in other) { // 元素是否相交                var obj_value = obj[k];                var other_value = other[k];                var obj_item_tag = _tostring(obj_value);                var other_item_tag = _tostring(other_value);                if (obj_item_tag === other_item_tag) {                    if (obj_item_tag === objectTag || obj_item_tag === arrayTag || other_item_tag === objectTag || other_item_tag === arrayTag) {                        return Equal(obj_value, other_value, equalp);                    }                    else {                        if (obj_value === other_value) {                            console_1.log('done.');                        }                        else {                            return false;                        }                    }                }                else {                    return false;                }            }            else {                return false;            }        }        return true;    }    return Equal(obj, other, equalp);}function equalp(obj, other) {    return equal(obj, other, true);}

调试

import {    log as l} from 'console';function eql(obj: any, other: any) {    return obj === other;}function equal(obj: any, other: any, equalp: boolean = false) {    const _tostring = (value: any): string => Object.prototype.toString.call(value);    const emptyp = function (value: any) {        return JSON.stringify(value).length === 2 ? true : false;    }    function Equal(obj: any, other: any, equalp: boolean): boolean {        let objTag = _tostring(obj);        let otherTag = _tostring(other);        let objectTag = '[object Object]'        let arrayTag = '[object Array]'        if (objTag !== objectTag && objTag !== arrayTag && otherTag !== objectTag && otherTag !== arrayTag) {            if (equalp && typeof obj === 'string' && typeof other === 'string') {                return (obj).toLocaleUpperCase() === (other).toLocaleUpperCase();            }            return obj === other;        }        if (objTag !== otherTag) return false;// 集合类型不一样        if (Object.getOwnPropertyNames(obj).length !== Object.getOwnPropertyNames(other).length) return false; // 集合元素数量不一样        if (emptyp(obj) && emptyp(other)) return true; // 类型一样的空集合,永远相等。        let data: any[] = (function () {            let data = Object.getOwnPropertyNames(obj);            if (objTag === arrayTag) {                data.pop()                return data            } else {                return data            }        })()        for (const i in data) {            const k = data[i];            if (k in other) { // 元素是否相交                let obj_value = obj[k];                let other_value = other[k];                let obj_item_tag = _tostring(obj_value);                let other_item_tag = _tostring(other_value);                if (obj_item_tag === other_item_tag) {                    if (obj_item_tag === objectTag || obj_item_tag === arrayTag || other_item_tag === objectTag || other_item_tag === arrayTag) {                        return Equal(obj_value, other_value, equalp);                    } else {                        if (obj_value === other_value) {                            l('done.');                        } else {                            return false;                        }                    }                } else {                    return false;                }            } else {                return false;            }        }        return true;    }    return Equal(obj, other, equalp)}function equalp(obj: any, other: any) {    return equal(obj, other, true);}l(equalp('hello', 'HELLo'))

转载于:https://www.cnblogs.com/ajanuw/p/9123787.html

你可能感兴趣的文章
汇编题目:在屏幕中间显示a-z的所有字母,按ESC键改变字符颜色
查看>>
AIM Tech Round (Div. 2) D. Array GCD dp
查看>>
Codeforces Round #249 (Div. 2) A B
查看>>
c++11 新特性之 autokeyword
查看>>
HDU 5627 Clarke and MST &意义下最大生成树 贪心
查看>>
jQuery html表格排序插件:tablesorter
查看>>
myeclipse10不用打开myeclipse configuration center安装插件的方法
查看>>
hbase自带mapreduce计数表行数功能
查看>>
Spring中自己主动装配
查看>>
数组去重复
查看>>
Swift - guard关键字(守护)
查看>>
sqlite数据库读写在linux下的权限问题
查看>>
sharepreference实现记住password功能
查看>>
http与https的区别
查看>>
【JavaScript】ReactJS&NodeJS了解资料
查看>>
springMVC demo搭建
查看>>
Android 百分比布局库(percent-support-lib) 解析与扩展
查看>>
JAXB完毕XML与Java对象的互转
查看>>
Android 自定义ViewGroup
查看>>
Apache Lucene(全文检索引擎)—搜索
查看>>