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. 疯狂JAVA——第七章 java基础类库

    System类代表当前java程序的运行平台,程序不能创建System类的对象,System类提供了一些类变量和类方法,允许直接通过System类来调用这些类变量和类方法.

  2. CentOS 下安装 OpenOffice4.0

    一.更新服务器 yum源 [root@APP2 /]# yum clean all [root@APP2 /]# yum makecache [root@APP2 /]# yum update 1.首 ...

  3. linux分区之gpt(大于2T的分区)

    1.文件系统限制: ext3块尺寸 最大文件尺寸 最大文件系统尺寸1KiB  16GiB  2TiB2KiB  256GiB  8TiB4KiB  2TiB  16TiB8KiB  16TiB  32 ...

  4. Java按值传递、按引用传递

    一般我们会说Java基本类型采用值传递,对象以及数组采用引用传递.但事实上这只是表面上的现象.实质上,Java都是按值传递引用.(Java中“引用”的概念相当于C++中的指针,可以不断改变值) 一,对 ...

  5. raptor

    raptor - 必应词典 美['ræptər]英['ræptə(r)] n.猛禽:攫禽 网络迅猛龙:雷电威龙:决战侏罗纪

  6. Matrix(二分套二分)

    Matrix http://poj.org/problem?id=3685 Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8 ...

  7. Largest Rectangle in a Histogram(附上几组测试数据)

    Largest Rectangle in a Histogram http://acm.hdu.edu.cn/showproblem.php?pid=1506 Time Limit: 2000/100 ...

  8. Linux 线程调度

    1.线程sleep()后,会让出cpu的时间片,交由其他线程进行抢占cpu. 线程之间正常的切换是依靠时间片的. 当主线程没有结束,且其在所占有的时间片内,并没有结束自己的工作,此时,子线程将会抢占c ...

  9. 两数之和-数据结构设计 · Two Sum - Data structure design

    [抄题]: 设计b并实现一个 TwoSum 类.他需要支持以下操作:add 和 find.add -把这个数添加到内部的数据结构.find -是否存在任意一对数字之和等于这个值 [思维问题]: 不知道 ...

  10. 硬盘的 read0 read 1

    Read 0:组建的时候必须2块容量相同的硬盘,每个程序的数据以一定的大小分别写在两个硬盘里,读的时候从两个硬盘里一起读,这种阵列方式理论上硬盘的读写速度是一块硬盘的2倍,实际应用中大约速度比一块硬盘 ...