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对象.在这中情况 ...
随机推荐
- curl -x 127.0.0.1:80
curl -x ip:80 +网址 就相当于在本地hosts文件指定一个域名,具有优先访问权.(curl -x 127.0.0.1:80这个方法适用于生产环境的服务器来测试自己做为代理商访问是否正常) ...
- Hive Word count
--https://github.com/slimandslam/pig-hive-wordcount/blob/master/wordcount.hql DROP TABLE myinput; DR ...
- Sample MultipleFileWordcount CombineFileInputFormat
在旧版本的samples中,使用的是旧的api,mapred下面的MultiFileInputFormat,现在已经过时. 现在推荐使用mapreduce下面的CombineInputFormat来处 ...
- c#分页读取GB文本文件
应用场景: a.我在做BI开发测试的时候,有可能面对source文件数GB的情况,如果使用一般的文本编辑器,则会卡死,或要等很久才能显示出来. b.有时候,我们使用ascii(01)或ascii(02 ...
- 2014 Super Training #3 H Tmutarakan Exams --容斥原理
原题: URAL 1091 http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有 ...
- Codeforces 402B --耻辱的一题
这题昨天晚上花了我1个小时50多分钟来搞,都没有搞定..后来看别人代码,直接暴力枚举第一个数的值来做..最多1000*1000的复杂度.当时怎么就没想到呢?还有为啥我的方法不对呢.. 暴力方法代码: ...
- ZOJ 2674 Strange Limit
欧拉函数. #include<iostream> #include<stdio.h> #include<string.h> #include<algorith ...
- centos6.5 redis3 开机自动启动命令设置
修改redis.conf,打开后台运行选项: # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note ...
- 常用Eclipse插件在线安装地址
Srping IDE http://www.springsource.com/update/e3.5 EasyShellhttp://pluginbox.sourceforge.net M2E ...
- Jira-Clone与发邮件的使用
1.克隆问题 包括两部分,先进行Clone,再进行移动 a.选择要克隆的问题,点击More Actions-Clone,在弹出框“复制问题”中,点击“创建”按钮即克隆成功 b.移动问题,点击More ...