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 script sleep synchronous

    function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { ...

  2. 大型运输行业实战_day07_1_订单查看实现

    1.业务分析 每个在窗口售票的售票员都应该可以随时查看自己的售票信息 简单的界面入口如图所示: 对应的html代码: <button onclick="orderDetail()&qu ...

  3. 查看webdriver针对浏览器的一些函数

    在用webdriver对浏览器进行操作时,很多操作并不是那么好找,后来在朋友的推荐下可以用下面的方法来寻找针对浏览器的一些操作,函数或属性等,这样方便我们可以查找一些方法去完成我们要的操作. 下面是查 ...

  4. css字体加粗

    参考 https://zhidao.baidu.com/question/2138403197991538308.html font-weight:bold;

  5. Telephone interview with Youyou Tu

    "Good News for the National Holiday!" Telephone interview with Youyou Tu following the ann ...

  6. luoguP1064 金明的预算方案 (有依赖的背包问题)

    题目链接:https://www.luogu.org/problemnew/show/P1064 这是一个有依赖的背包问题,属于01背包的变式.这题还好,每个主件最多有2个附件,那么在对主件进行背包的 ...

  7. 配置Maven从私服下载构件

    --------------------siwuxie095                                     配置 Maven 从私服下载构件         从 Nexus ...

  8. 兼容多浏览器的网页复制插件(ZeroClipboard)

    前言: 常规利用JS编写的网页复制功能是最简单的方法,但是只对IE有效,无法做到兼容其它浏览器,对其他浏览器也就只能弹窗提示用户手动复制了. <script type="text/ja ...

  9. Mybatis的分页插件PageHelp:Page对象中的pageSize等属性无法序列化,无法转换为json字符串

    Page<User> page = new Page<>();        User user = new User();        user.setAge(20);   ...

  10. 2-Qt关闭子窗口时执行特定代码

    https://blog.csdn.net/naibozhuan3744/article/details/82689434 本文主要总结在关闭qt的QWidget子窗口瞬间,执行特定代码.由于主窗口关 ...