js函数 eql,equal,equalp
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'))
function equal(obj, other) {
const objectTag = "[object Object]";
const arrayTag = "[object Array]";
const _tostring = value => Object.prototype.toString.call(value);
const emptyp = value => JSON.stringify(value).length === 2;
// 记录所有的对象
function Equal(obj, other) {
let objTag = _tostring(obj);
let otherTag = _tostring(other);
// 非集合,使用===判断
if (
objTag !== objectTag &&
objTag !== arrayTag &&
otherTag !== objectTag &&
otherTag !== arrayTag
) {
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 rsult = false;
for (const k in obj) {
if (k in other) {
const obj_value = obj[k];
const other_value = other[k];
rsult = Equal(obj_value, other_value);
} else {
return false;
}
}
return rsult;
}
return Equal(obj, other);
}
js函数 eql,equal,equalp的更多相关文章
- api日常总结:前端常用js函数和CSS常用技巧
我的移动端media html{font-size:10px} @media screen and (min-width:321px) and (max-width:375px){html{font- ...
- 3.3 js函数
1.函数语法: 函数声明的方式:function 函数名(参数1,参数2-){//函数体;}函数调用:函数名(参数1,参数2-); 函数内不一定都指定返回值. 如果需要指定返回值,可用 return ...
- Js函数function基础理解
正文:我们知道,在js中,函数实际上是一个对象,每个函数都是Function类型的实例,并且都与其他引用类型一样具有属性和方法.因此,函数名实际上是指向函数对象的指针,不与某个函数绑定.在常见的两种定 ...
- js函数表达式和函数声明的区别
我们已经知道,在任意代码片段外部添加包装函数,可以将内部的变量和函数定义"隐 藏"起来,外部作用域无法访问包装函数内部的任何内容. 例如: var a = 2; function ...
- 通用js函数集锦<来源于网络> 【二】
通用js函数集锦<来源于网络> [二] 1.数组方法集2.cookie方法集3.url方法集4.正则表达式方法集5.字符串方法集6.加密方法集7.日期方法集8.浏览器检测方法集9.json ...
- 通用js函数集锦<来源于网络/自己> 【一】
通用js函数集锦<来源于网络/自己>[一] 1.返回一个全地址2.cookie3.验证用户浏览器是否是微信浏览器4.验证用户浏览器是否是微博内置浏览器5.query string6.验证用 ...
- 100多个基础常用JS函数和语法集合大全
网站特效离不开脚本,javascript是最常用的脚本语言,我们归纳一下常用的基础函数和语法: 1.输出语句:document.write(""); 2.JS中的注释为//3.传统 ...
- JS函数
1.document.write(""); 输出语句2.JS中的注释为//3.传统的HTML文档顺序是:document->html->(head,body)4.一个浏 ...
- js函数和运算符
函数是由事件驱动或者它被调用时执行可重复使用的代码块. <script> function myFunction(){ Alert(“hello World!”): } </scri ...
随机推荐
- [Unit Testing] Using Mockito Annotations - @Mock, @InjectMocks, @RunWith
Previously we have seen how to do Unit testing with Mockito; import org.junit.Test; import static or ...
- build.gradle最佳实践之buildConfigField
使用AndroidStudio进行开发,其中很重要的一个文件就是build.gradle,他是整个项目的控制中心,这里收集一些日常会用到的语法或者使用技巧,以备后用.这篇博客主要说明 buildTyp ...
- 微软BI 之SSIS 系列 - 通过 ROW_NUMBER 或 Script Component 为数据流输出添加行号的方法
开篇介绍 上午在天善回答看到这个问题 - SSIS 导出数据文件,能否在第一列增加一个行号,很快就帮助解决了,方法就是在 SQL 查询的时候加一个 ROW_NUMBER() 就可以了. 后来想起在两年 ...
- Dive into Python
写这篇文章的原因完全是为了督促自己每天晚上看完两章<Dive Into Python>这本书,因此,很多内容都是摘抄自原书的翻译版或者是自己瞎想,于是就顺带着记录了下来.此前已经看完前两章 ...
- ARM与X86 CPU架构对比区别
CISC(复杂指令集计算机)和RISC(精简指令集计算机)是当前CPU的两种架构.它们的区别在于不同的CPU设计理念和方法.早期的CPU全部是CISC架构,它的设计目的是 CISC要用最少的机器语言 ...
- linux下无法执行PHP命令,错误 php: command not found
在linux下执行php时无法执行,报错:php: command not found 解决方法: export PATH=$PATH:/usr/local/php7/bin 可以输入echo $PA ...
- 【转载】Mysql load data infile用法(万级数据导入,在几秒之内)
https://blog.csdn.net/u014082714/article/details/53173975 http://blog.itpub.net/26506993/viewspace-2 ...
- [docker]macvlan实现双vlan互通
关于vlan的冷知识 vlan范围:0~4095 0,4095 保留 仅限系统使用 用户不能查看和使用这些VLAN 1 正常 Cisco默认VLAN 用户能够使用该VLAN,但不能删除它 2-1001 ...
- 全面理解Javascript闭包和闭包的几种写法及用途【转】
一.什么是闭包和闭包的几种写法和用法 1.什么是闭包 闭包,官方对闭包的解释是:一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分.闭包的特点: 1. ...
- lua -- 在弹框中显示物品列表
-- 宝箱打开后,显示里面的物品列表 -- 显示方式是:一个一个显示,每三个一行,一行显示完就移动到下一行, -- 接下来的一样要显示框的中间位置,方便玩家看 function UIBagContro ...