angular的工具方法笔记(equals, HashKey)
分别是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>
angular的工具方法笔记(equals, HashKey)的更多相关文章
- 秒味课堂Angular js笔记------Angular js中的工具方法
Angular js中的工具方法 angular.isArray angular.isDate angular.isDefined angular.isUndefined angular.isFunc ...
- Java程序猿的JavaScript学习笔记(9—— jQuery工具方法)
计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...
- vue.js 源代码学习笔记 ----- 工具方法 env
/* @flow */ /* globals MutationObserver */ import { noop } from 'shared/util' // can we use __proto_ ...
- jQuery笔记之工具方法extend插件扩展
jQuery工具方法 $.extend()插件扩展(工具方法) $.fn.extend()插件扩展(实例方法) 浅度克隆.深度克隆 两个方法基本是一样的,唯一不同的就是调用方式不一样 -------- ...
- jQuery笔记之工具方法
jQuery 工具方法 $.type()判断数据类型 $.isArray() $.isFunction() $.isWindow()... $.trim()消除空格 $.proxy()改变this指向 ...
- JQuery --- 第一期 (初识jQuery, JQuery核心函数和工具方法)
个人学习笔记 初识jQuery 1.我的第一个JQuery <!DOCTYPE html> <html lang="en"> <head> & ...
- Utils工具方法集插件详解
var Utils = function(){}; Utils.text = { stripTags: function (val) { return val.replace(/<\/?[^&g ...
- AngularJS的核心对象angular上的方法全面解析(AngularJS全局API)
总结一下AngularJS的核心对象angular上的方法,也帮助自己学习一下平时工作中没怎么用到的方法,看能不能提高开发效率.我当前使用的Angularjs版本是1.5.5也是目前最新的稳定版本,不 ...
- JQuery操作类数组的工具方法
JQuery学习之操作类数组的工具方法 在很多时候,JQuery的$()函数都返回一个类似数据的JQuery对象,例如$('div')将返回div里面的所有div元素包装的JQuery对象.在这中情况 ...
随机推荐
- redis watch multi exec 关系
EXEC 执行所有事务块内的命令. 假如某个(或某些) key 正处于 WATCH 命令的监视之下,且事务块中有和这个(或这些) key 相关的命令,那么EXEC 命令只在这个(或这些) key 没有 ...
- pig 介绍与pig版 hello world
前两天使用pig做ETL,粗浅的看了一下,没有系统地学习,感觉pig还是值得学习的,故又重新看programming pig. 以下是看的第一章的笔记: What is pig? Pig provid ...
- 使用reportNG替换testNG的默认报告
关于reportng的官网介绍:http://reportng.uncommons.org/ 1.下载reportNG的jar包:http://pan.baidu.com/s/1hq5znLU 2.r ...
- 该如何认识ZBrush中的2.5D绘画
ZBrush不仅对3D行业进行了改革.让艺术家感到无约束自由创作的3D设计,同时它还是一个强大的绘画程序!基于强大的Pixol功能,ZBrush®将数字绘画提升到一个新的层次.如下图所示,插画功能主要 ...
- POJ 3250 Bad Hair Day --单调栈(单调队列?)
维护一个单调栈,保持从大到小的顺序,每次加入一个元素都将其推到尽可能栈底,知道碰到一个比他大的,然后res+=tail,说明这个cow的头可以被前面tail个cow看到.如果中间出现一个超级高的,自然 ...
- JavaWeb学习----JSP简介及入门(含Eclipse for Java EE及Tomcat的配置)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- 第24章 SEH结构化异常处理_异常处理及软件异常
24.1 程序的结构 (1)try/except框架 __try{ //被保护的代码块 …… } __except(except fileter/*异常过滤程序*/){ //异常处理程序 } (2) ...
- 从Maya中导入LightMap到unity中
导入步骤 1.在Maya中为每一个模型烘焙好帖图(tif格式),会发现烘焙好的图和UV是一一对应的 2.把模型和烘焙帖图导入到Unity中 3.选中材质,修改Shader为 Legacy Shader ...
- MonoDevelop Debug Unity
环境 Unity 4.3.x MonoDevelop 4.0.1 资料 更新Unity4.3.X之后的版本,MonoDevelop的版本也进行了升级,IDE的界面发生了比较大的改变. 查阅了Unity ...
- 转: CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法
from: http://www.linuxde.net/2014/05/15576.html CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法 2014/05/ ...