码迷,mamicode.com
首页 > 其他好文 > 详细

angular的工具方法笔记(equals, HashKey)

时间:2014-12-17 20:33:31      阅读:1133      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   使用   sp   

  分别是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)

标签:style   blog   http   ar   io   color   os   使用   sp   

原文地址:http://www.cnblogs.com/diligenceday/p/4170063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!