分别是angular脏值检测的工具方法equals和 类HashKey的使用方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ng</title>
</head>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>

<body>
<script>
/**
* Created by duanyao on 14-12-12.
*/
function isUndefined(value){return typeof value == 'undefined';} function isDefined(value){return typeof value != 'undefined';} function isObject(value){return value != null && typeof value == 'object';} function isString(value){return typeof value == 'string';} function isNumber(value){return typeof value == 'number';} function isDate(value){
return toString.apply(value) == '[object Date]';
} function isArray(value) {
return toString.apply(value) == '[object Array]';
} function isFunction(value){return typeof value == 'function';} function isWindow(obj) {
return obj && obj.document && obj.location && obj.alert && obj.setInterval;
} function isScope(obj) {
return obj && obj.$evalAsync && obj.$watch;
} function isFile(obj) {
return toString.apply(obj) === '[object File]';
} function isBoolean(value) {
return typeof value == 'boolean';
} //匹配两个元素是否相等, 要注意一下:;
//equals({a:1,b:function(){2222222222}},{a:1,b:function(){11111111}}) ==>> true;
function equals(o1, o2) {
if (o1 === o2) return true;
if (o1 === null || o2 === null) return false;
if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN
var t1 = typeof o1, t2 = typeof o2, length, key, keySet;
if (t1 == t2) {
if (t1 == 'object') {
if (isArray(o1)) {
if ((length = o1.length) == o2.length) {
for(key=0; key<length; key++) {
if (!equals(o1[key], o2[key])) return false;
}
return true;
}
} else if (isDate(o1)) {
return isDate(o2) && o1.getTime() == o2.getTime();
} else {
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2)) return false;
keySet = {};
for(key in o1) {
if (key.charAt(0) === '$' || isFunction(o1[key])) continue;
if (!equals(o1[key], o2[key])) return false;
keySet[key] = true;
}
for(key in o2) {
if (!keySet[key] &&
key.charAt(0) !== '$' &&
o2[key] !== undefined &&
!isFunction(o2[key])) return false;
}
return true;
}
}
}
return false;
}; var uid = ['0', '0', '0'];
function nextUid() {
var index = uid.length;
var digit; while(index) {
index--;
digit = uid[index].charCodeAt(0);
if (digit == 57 /*'9'*/) {
uid[index] = 'A';
return uid.join('');
}
if (digit == 90 /*'Z'*/) {
uid[index] = '0';
} else {
uid[index] = String.fromCharCode(digit + 1);
return uid.join('');
}
}
uid.unshift('0');
return uid.join('');
}
/*
hashKey(1) ==》 "number:1"
hashKey(2) ==》 "number:2"
hashKey("hehe") ==》 "string:hehe"
hashKey({1:1}) ==》 "object:001"
hashKey({1:2}) ==》 "object:002
* */
function hashKey(obj) {
var objType = typeof obj,
key; if (objType == 'object' && obj !== null) {
//如果不是纯对象自己有$$hashKey;
if (typeof (key = obj.$$hashKey) == 'function') {
// must invoke on object to keep the right this
key = obj.$$hashKey();
} else if (key === undefined) {
key = obj.$$hashKey = nextUid();
}
} else {
key = obj;
} return objType + ':' + key;
} /**
* HashMap which can use objects as keys
*/
function HashMap(array){
forEach(array, this.put, this);
}
HashMap.prototype = {
/**
* Store key value pair
* @param key key to store can be any type
* @param value value to store can be any type
*/
put: function(key, value) {
//通过hashKey传进去对象或者字符串返回的都是唯一不重复的一个字符串的哈希值;
this[hashKey(key)] = value;
}, /**
* @param key
* @returns the value for the key
*/
get: function(key) {
return this[hashKey(key)];
}, /**
* Remove the key/value pair
* @param key
*/
remove: function(key) {
var value = this[key = hashKey(key)];
delete this[key];
return value;
}
}; //HashMap是这么用的;
var modules = [{ngLocale: {1:1,2:2}}, {ng: {2:2,3:3}}, {phonecatApp: {2:2,33:33}}]
var loadedModules = new HashMap();
$.each(modules,function(index,module){
loadedModules.put(module, true);
});
</script>
</body>
</html>

最新文章

  1. serv-u启动管理控制台后提示脚本错误解决方案
  2. oracle创建包后执行报错:object omgmig.test_package is invalid.
  3. Windows远程桌面连接如何直接使用剪贴板功能
  4. PythonDay01
  5. 【学习笔记】【C语言】字符串数组
  6. What is Object Oriented Design? (OOD)
  7. Qt之启动外部程序
  8. 如何将js与HTML完全脱离
  9. canvas基础—图形变换
  10. python 模块中的 __init__.py __main__.py
  11. python运算符和数据类型的可变性
  12. MJRefresh在Xode6中报错处理
  13. Android IPC机制(三)使用AIDL实现跨进程方法调用
  14. FPGA中亚稳态相关问题及跨时钟域处理
  15. Python Flask之留言板(无数据库)
  16. mac-禅道环境
  17. 每月IT摘录201810
  18. Maven hive-jdbc教程
  19. Flask从入门到精通之使用Flask-SQLAlchemy管理数据库
  20. Nginx使用记录

热门文章

  1. MAC、IDFA、IMEI正则表达式
  2. php 利用activeMq+stomp实现消息队列
  3. 动手学习TCP:TCP连接建立与终止
  4. 【对比分析八】null和undefined的区别
  5. uva 558 tree(不忍吐槽的题目名)——yhx
  6. Codeforces 13C Sequence --DP+离散化
  7. 在springmvc中使用hibernate-validate
  8. Linux下如何知道文件被那个进程写
  9. Spring Mvc 在非controller层 实现获取request对象
  10. Wordpress 3.5.1的debug流水账