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>
时间: 2024-08-05 09:58:42

angular的工具方法笔记(equals, HashKey)的相关文章

Java程序员的JavaScript学习笔记(9—— jQuery工具方法)

计划按如下顺序完成这篇笔记: 1.    理念. 2.    属性复制和继承. 3.    this/call/apply. 4.    闭包/getter/setter. 5.    prototype. 6.    面向对象模拟. 7.    jQuery基本机制. 8.    jQuery选择器. 9.    jQuery工具方法. 10.    jQuery-在"类"层面扩展. 11.    jQuery-在"对象"层面扩展. 12.    jQuery-扩

Java程序猿的JavaScript学习笔记(9—— jQuery工具方法)

计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript学习笔记(3--this/call/apply) Java程序猿的JavaScript学习笔记(4--this/闭包/getter/setter) Java程序猿的JavaScript学习笔记(5--prototype) Java程序猿的JavaScript学习笔记(6--面向对象模拟) Java程

angularjs中常用的工具方法

一.angular.bind(self, fn, args) 1.作用:返回一个新的函数,绑定这个函数的this指向self 2.参数: self:新函数的上下文对象 fn:需要绑定的函数 args:传递给函数的参数 3.返回值:this指向self的新函数 4.备注:bind会根据你的参数类型来决定调用call或apply,所以args可以是一个个数据,也可以是一个数组. 二.angular.copy(source, [destination]) 1.作用:对象的深拷贝 2.参数: sourc

angularJs中的常用工具方法

前面说过在angularJs中使用 angular.module() 法可创建一个angularJs模块.除此之外,angularJs还提供了一些工具方法供我们使用. angular.isArray() //判断传入的参数是不是数组,是则返回true 否则返回false angular.isDate() //判断传入的参数是不是时间对象,是则返回true,否则返回false angular.isFunction() //判断传入的参数是不是函数,是则返回true,否则返回false angula

AngularJS 工具方法以及AngularJS中使用jQuery

1. AngularJS 工具方法,参考angularjs API https://docs.angularjs.org/api官方文档 (1)angular.isArray(value) 判断是否是数组,返回true/false [html] view plain copy <div ng-controller="firstController">{{isArray}}</div> [html] view plain copy $scope.arr=[1,2,

jquery源码之工具方法

jQuery 作为时下前端的"霸主".它的强大已毋庸置疑.简洁,效率,优雅,易用等优点让人很容易对它珍爱有加. 作为js的小菜,为了提升自我等级,根据各大神博客精辟的解析,硬啃了jQuery源码.在此,并不是要解析啥源码啥的(也没到那个级别哈),读书笔记,仅此而已. 所谓磨刀不误砍柴功,jQuery在大展神通之前也做了许多准备工作.比如说他的一些工具方法: 首当其冲的是他的继承扩展方法: jQuery.extend 其实也不是传统意义的继承,说mixin可能更恰当一些. // 首先看看

Utils工具方法集插件详解

var Utils = function(){}; Utils.text = { stripTags: function (val) { return val.replace(/<\/?[^>]+>/gi, "");   //正则表达式中的<\/?其实就是匹配</或<,后面的[^>]+其实就是匹配不是>的任何一个或多个字符,因此此正则可以匹配<div>,</div>,<input type="tex

文件操作常用工具方法

写字节到文件: /** * 工具方法,写bytes到文件中 如果写入过程出现异常就删除文件 * * @param bytes * @param file */ public static void writeBytesToFile(byte[] bytes, File file) { RandomAccessFile access = null; try { access = new RandomAccessFile(file, "rw"); access.write(bytes);

java常用工具方法2

/* * Copyright 2005 Joe Walker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LI