一、 mixError用来给出错误
function minErr(module, ErrorConstructor) { ErrorConstructor = ErrorConstructor || Error; return function() { var code = arguments[0], prefix = '[' + (module ? module + ':' : '') + code + '] ', template = arguments[1], templateArgs = arguments, message, i; message = prefix + template.replace(/{d+}/g, function(match) { var index = +match.slice(1, -1), arg; if (index + 2 < templateArgs.length) { return toDebugString(templateArgs[index + 2]); } return match; }); message = message + ' http://errors.angularjs.org/1.3.9-local+sha.a3c3bf3/' + (module ? module + '/' : '') + code; for (i = 2; i < arguments.length; i++) { message = message + (i == 2 ? '?' : '&') + 'p' + (i - 2) + '=' + encodeURIComponent(toDebugString(arguments[i])); } return new ErrorConstructor(message); }; }
上面一段代码用到的输出toDebugString和序列化对象serializeObject如下:
/* global: toDebugString: true */ function serializeObject(obj) { var seen = []; return JSON.stringify(obj, function(key, val) { val = toJsonReplacer(key, val); if (isObject(val)) { if (seen.indexOf(val) >= 0) return '<<already seen>>'; seen.push(val); } return val; }); } function toDebugString(obj) { if (typeof obj === 'function') { return obj.toString().replace(/ {[sS]*$/, ''); } else if (typeof obj === 'undefined') { return 'undefined'; } else if (typeof obj !== 'string') { return serializeObject(obj); } return obj; }
二、
var lowercase = function(string) {return isString(string) ? string.toLowerCase() : string;}; var hasOwnProperty = Object.prototype.hasOwnProperty; var uppercase = function(string) {return isString(string) ? string.toUpperCase() : string;}; var manualLowercase = function(s) { /* jshint bitwise: false */ return isString(s) ? s.replace(/[A-Z]/g, function(ch) {return String.fromCharCode(ch.charCodeAt(0) | 32);}) : s; }; var manualUppercase = function(s) { /* jshint bitwise: false */ return isString(s) ? s.replace(/[a-z]/g, function(ch) {return String.fromCharCode(ch.charCodeAt(0) & ~32);}) : s; };
if ('i' !== 'I'.toLowerCase()) {
lowercase = manualLowercase;
uppercase = manualUppercase;
}
三、 变量定义
var msie, // holds major version number for IE, or NaN if UA is not IE. jqLite, // delay binding since jQuery could be loaded after us. jQuery, // delay binding slice = [].slice, splice = [].splice, push = [].push, toString = Object.prototype.toString, ngMinErr = minErr('ng'), /** @name angular */ angular = window.angular || (window.angular = {}), angularModule, uid = 0; /** * documentMode is an IE-only property * http://msdn.microsoft.com/en-us/library/ie/cc196988(v=vs.85).aspx */ msie = document.documentMode;
四
function isArrayLike(obj) { if (obj == null || isWindow(obj)) { return false; } var length = obj.length; if (obj.nodeType === NODE_TYPE_ELEMENT && length) { return true; } return isString(obj) || isArray(obj) || length === 0 || typeof length === 'number' && length > 0 && (length - 1) in obj; }
五、遍历对象,对其中的属性执行遍历函数。还有序列化,以及序列化后的遍历
function forEach(obj, iterator, context) { var key, length; if (obj) { if (isFunction(obj)) { for (key in obj) { // Need to check if hasOwnProperty exists, // as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function if (key != 'prototype' && key != 'length' && key != 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) { iterator.call(context, obj[key], key, obj); } } } else if (isArray(obj) || isArrayLike(obj)) { var isPrimitive = typeof obj !== 'object'; for (key = 0, length = obj.length; key < length; key++) { if (isPrimitive || key in obj) { iterator.call(context, obj[key], key, obj); } } } else if (obj.forEach && obj.forEach !== forEach) { obj.forEach(iterator, context, obj); } else { for (key in obj) { if (obj.hasOwnProperty(key)) { iterator.call(context, obj[key], key, obj); } } } } return obj; }
function sortedKeys(obj) { return Object.keys(obj).sort(); }
function forEachSorted(obj, iterator, context) { var keys = sortedKeys(obj); for (var i = 0; i < keys.length; i++) { iterator.call(context, obj[keys[i]], keys[i]); } return keys; }
六、 变化参数位置
function reverseParams(iteratorFn) { return function(value, key) { iteratorFn(key, value); }; }
七、 唯一id生成
function nextUid() { return ++uid; }
八、设置对象的hashkey
function setHashKey(obj, h) { if (h) { obj.$$hashKey = h; } else { delete obj.$$hashKey; } }
九、 给对象扩展属性,多个复制到一个
function extend(dst) { var h = dst.$$hashKey; for (var i = 1, ii = arguments.length; i < ii; i++) { var obj = arguments[i]; if (obj) { var keys = Object.keys(obj); for (var j = 0, jj = keys.length; j < jj; j++) { var key = keys[j]; dst[key] = obj[key]; } } } setHashKey(dst, h); return dst; }
十、转化为数字,和继承扩展
function int(str) { return parseInt(str, 10); } function inherit(parent, extra) { return extend(Object.create(parent), extra); }
十一 空函数和函数的存值,并应用一个
function noop() {} noop.$inject = []; function identity($) {return $;} identity.$inject = [];
十二、
function valueFn(value) {return function() {return value;};}
十三、各种判断
function isUndefined(value) {return typeof value === 'undefined';} function isDefined(value) {return typeof value !== 'undefined';} function isObject(value) { // http://jsperf.com/isobject4 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.call(value) === '[object Date]'; } var isArray = Array.isArray; function isFunction(value) {return typeof value === 'function';} function isRegExp(value) { return toString.call(value) === '[object RegExp]'; } unction isWindow(obj) { return obj && obj.window === obj; } function isScope(obj) { return obj && obj.$evalAsync && obj.$watch; } function isFile(obj) { return toString.call(obj) === '[object File]'; } function isFormData(obj) { return toString.call(obj) === '[object FormData]'; } function isBlob(obj) { return toString.call(obj) === '[object Blob]'; } function isBoolean(value) { return typeof value === 'boolean'; } function isPromiseLike(obj) { return obj && isFunction(obj.then); }
十四、
var trim = function(value) { return isString(value) ? value.trim() : value; }; var escapeForRegexp = function(s) { return s.replace(/([-()[]{}+?*.$^|,:#<!\])/g, '\$1').replace(/x08/g, '\x08'); };