1、_.keys():获取对象的属性名,不包含原型链

_.keys = nativeKeys || function(obj) {
if (obj !== Object(obj)) throw new TypeError('Invalid object');
var keys = [];
//都是用自增函数
for (var key in obj) if (_.has(obj, key)) keys[keys.length] = key;
return keys;
};

2、_.values():返回对象的值,不包含原型链的值

_.values = function(obj) {
//对函数执行_.identity,并返回数组
return _.map(obj, _.identity);
};

3、_.functions():返回对象所有的方法名

_.functions = _.methods = function(obj) {
var names = [];
for (var key in obj) {
if (_.isFunction(obj[key])) names.push(key);
}
return names.sort();
};

4、_.extend():复制source对象中的所有属性覆盖到destination对象上,并且返回 destination 对象. 复制是按顺序的

_.extend = function(obj) {
//会传入多个source
each(slice.call(arguments, 1), function(source) {
for (var prop in source) {
obj[prop] = source[prop];
}
});
return obj;
};

5、_.pick():过滤obj,返回指定key的对象

_.pick = function(obj) {
var result = {};
each(_.flatten(slice.call(arguments, 1)), function(key) {
if (key in obj) result[key] = obj[key];
});
return result;
};

6、_.defaults():用defaults对象填充object中undefined属性。并且返回这个object。

_.defaults = function(obj) {
each(slice.call(arguments, 1), function(source) {
for (var prop in source) {
if (obj[prop] == null) obj[prop] = source[prop];
}
});
return obj;
};

7、_.clone():浅复制object,

_.clone = function(obj) {
if (!_.isObject(obj)) return obj;
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
};

7、_.tap(object, interceptor):用 object作为参数来调用函数interceptor,然后返回object。链式调用时很有用

实例
_.chain([1,2,3,200])
.filter(function(num) { return num % 2 == 0; })
.tap(alert)
.map(function(num) { return num * num })
.value();
_.tap = function(obj, interceptor) {
interceptor(obj);
return obj;
};

8、eq():比较两个数据的值,是否相等

9、_.isEqual(); 内部函数eq的外部用法

_.isEqual = function(a, b) {
return eq(a, b, []);
};

10、_.isEmpty():测验:'', false, 0, null, undefined, NaN, [], {}

_.isEmpty = function(obj) {
   //null, undefined
if (obj == null) return true;
   //'',[]
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
for (var key in obj) if (_.has(obj, key)) return false;
   //false,0,NaN
return true;
};

11、_.isElement():验证对象是否是一个DOM对象

    _.isElement = function(obj) {
     //为什么要写!!
return !!(obj && obj.nodeType == 1);
};

12、_.isArray():验证对象是否是一个数组类型, 优先调用宿主环境提供的isArray方法;isFunction;isString;isNumber,isDate,isRegExp;同样

    _.isArray = nativeIsArray ||
function(obj) {
return toString.call(obj) == '[object Array]';
};

13、_.isObject():证对象是否是一个复合数据类型的对象(即非基本数据类型String, Boolean, Number, null, undefined)

如果基本数据类型通过new进行创建, 则也属于对象类型

    _.isObject = function(obj) {
return obj === Object(obj);
};

14、_.isArguments():检查一个数据是否是一个arguments参数对象

    _.isArguments = function(obj) {
return toString.call(obj) == '[object Arguments]';
};
// 验证isArguments函数, 如果运行环境无法正常验证arguments类型的数据, 则重新定义isArguments方法
//还可以这样...
if(!_.isArguments(arguments)) {
// 对于环境无法通过toString验证arguments类型的, 则通过调用arguments独有的callee方法来进行验证
_.isArguments = function(obj) {
// callee是arguments的一个属性, 指向对arguments所属函数自身的引用
       //有这个属性,就是arguments
return !!(obj && _.has(obj, 'callee'));
};
}

15、_.isFunction():判断是否为函数

_.isFunction = function(obj) {
return toString.call(obj) == '[object Function]';
};

16、_.isString():验证对象是否是一个字符串类型

_.isString = function(obj) {
return toString.call(obj) == '[object String]';
};

17、_.isNumber():验证对象是否是一个数字类型

_.isNumber = function(obj) {
return toString.call(obj) == '[object Number]';
};

18_.isFinite(): 检查一个数字是否为有效数字且有效范围(Number类型, 值在负无穷大 - 正无穷大之间)

_.isFinite = function(obj) {
     //isFinite()是window函数
return _.isNumber(obj) && isFinite(obj);
};

19、_.isNaN(): 检查数据是否为NaN类型(所有数据中只有NaN与NaN不相等)

    _.isNaN = function(obj) {
return obj !== obj;
};

20、 _.isBoolean():检查数据是否为Boolean类型

    _.isBoolean = function(obj) {
// 支持字面量和对象形式的Boolean数据
return obj === true || obj === false || toString.call(obj) == '[object Boolean]';
};

21、_.isDate():检查数据是否是一个Date类型

_.isDate = function(obj) {
return toString.call(obj) == '[object Date]';
};

22、_.isRegExp():检查数据是否是一个正则表达式类型

_.isRegExp = function(obj) {
return toString.call(obj) == '[object RegExp]';
};

23、_.isNull():检查数据是否为null

 _.isNull = function(obj) {
return obj === null;
};

24、_.isUndefined():检查数据是否是Undefined(未定义的)值

    _.isUndefined = function(obj) {
return obj === void 0;
};

25、_.has():对象本身是否包含指定的属性,不检查原型链,是hasOwnProperty的安全封装

_.has = function(obj, key) {
   //其实这种不安全,后面的版本会改进
return hasOwnProperty.call(obj, key);
};

  

underscore objects的更多相关文章

  1. Object Pascal中文手册 经典教程

    Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...

  2. (三)underscore.js框架Objects类API学习

    keys_.keys(object)  Retrieve all the names of the object's properties. _.keys({one: 1, two: 2, three ...

  3. JavaScript 特殊对象 Array-Like Objects 详解

    这篇文章拖了有两周,今天来跟大家聊聊 JavaScript 中一类特殊的对象 -> Array-Like Objects. (本文节选自 underscore 源码解读系列文章,完整版请关注 h ...

  4. 你可能不知道的 NaN 以及 underscore 1.8.3 _.isNaN 的一个 BUG

    这篇文章并不在我的 underscore 源码解读计划中,直到 @pod4g 同学回复了我的 issue(详见 https://github.com/hanzichi/underscore-analy ...

  5. 【跟着子迟品 underscore】Object Functions 相关源码拾遗 & 小结

    Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...

  6. 【跟着子迟品 underscore】JavaScript 中如何判断两个元素是否 "相同"

    Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...

  7. 你可能不再需要Underscore

    过去几年像 Underscore 和 lodash 等库进入许多JavaScript程序员的工具函数中.虽然这些工具库可以使你的代码写起来更容易,但是他们不一定使代码更简单或更容易理解. 各种工具函数 ...

  8. Underscore.js 初探

    一. 简介   Underscore 这个单词的意思是“下划线”.   Underscore.js 是一个 JavaScript 工具库,提供了一整套的辅助方法供你使用.   Think that - ...

  9. Lo-Dash – 替代 Underscore 的优秀 JS 工具库

    前端开发人员大都喜欢 Underscore,它的工具函数很实用,用法简单.这里给大家推荐另外一个功能更全面的 JavaScript 工具——Lo-Dash,帮助你更好的开发网站和 Web 应用程序. ...

随机推荐

  1. JFR 与 JProfilter Jvmisualvm

    只有JFR 是可以在生产环境使用  采用C++独立写的采样手机功能 而 JProfilter/JVisualvm 都只能在测试环境下使用 使用instrument 机制 ,还有debug 框架 最早是 ...

  2. BIGDECIMAL 四舍五入等取舍问题

    我输入的是1.35,但是电脑不可能取到整数,他的值如下:初始化数据:1.350000000000000088817841970012523233890533447265625ROUND_DOWN); ...

  3. Bioconductor的历史

    ---------------------------------------------------------------Bioconductor------------------------- ...

  4. 如何获取某个网站的favicon.ico

    http://moco.imooc.com/player/report.html 今天看到这个网站上,左侧的小图片挺好看的,想弄下来,检查源码,也没有看到 <head> <meta  ...

  5. thymeleaf 拼接字符串与变量

    参考https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html <span th:text="'The name o ...

  6. centos7 源码编译安装TensorFlow CPU 版本

    一.前言 我们都知道,普通使用pip安装的TensorFlow是万金油版本,当你运行的时候,会提示你不是当前电脑中最优的版本,特别是CPU版本,没有使用指令集优化会让TensorFlow用起来更慢. ...

  7. 获取APP的启动图 -Launch Image

    http://adad184.com/2015/10/15/tips-access-current-launch-image/

  8. SQL存储过程将符合条件的大量记录批量删除脚本

    -- ============================================= -- Author: James Fu -- Create date: 2015/10/27 -- D ...

  9. Nginx+Keepalived实现站点高可用[z]

    http://segmentfault.com/a/1190000002881132

  10. poj3017 Cut the Sequence 单调队列 + 堆 dp

    描述 把一个正数列 $A$分成若干段, 每段之和 不超过 $M$, 并且使得每段数列的最大值的和最小, 求出这个最小值. 题目链接 题解 首先我们可以列出一个$O(n^2)$ 的转移方程 : $F_i ...