本文为原创作品,可以转载,但请添加本文连接,谢谢传阅,本人博客已转移至github,地址为:jruif.github.io

underscorejs,一个实用的的Javascript函数库,值得推荐,官网地址Github仓库有注释的源码

  • obj.length === +obj.length 判断obj.length是不是一个数字,“+”会吧非number类型的值尝试转换为number类型,如果失败返回NAN。
  • void 0 这个相信大家经常见,但是你明白它是做什么的吗?而且我们遇到的情况大多都是在超链接里写着Javascript:(void 0),现在我又遇到了a === void 0,好吧,不买官子了,其实这个是用来防止undefined被重置(关于这一点可以点击这里查看),而void是一个修饰参数的前缀关键字,并且永远返回undefined,因此在超链接里使用void 0就清晰了,返回undefined就阻止了a标签的默认事件。例如:
void 0
void (0)
void "hello"
void (new Date())
//都将返回undefined

为什么使用0,我只想说呵呵,谁让0最短小可爱呢。

  • ECMAScript5中的bind,underscore的实现方法
var nativeBind = FuncProto.bind;
var Ctor = function(){};
_.bind = function(func, context) {
var args, bound;
if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, Array.prototype.slice.call(arguments, 1));
if (!_.isFunction(func)) throw new TypeError('Bind must be called on a function');
args = Array.prototype.slice.call(arguments, 2);
bound = function() {
if (!(this instanceof bound)) return func.apply(context, args.concat(Array.prototype.slice.call(arguments)));
Ctor.prototype = func.prototype;
var self = new Ctor;
Ctor.prototype = null;
var result = func.apply(self, args.concat(Array.prototype.slice.call(arguments)));
if (_.isObject(result)) return result;
return self;
};
return bound;
};

  bind很多人不明白为什么在有了call和apply还是要出个bind,看完这段代码大家应该明白了吧,其实就是内存驻留版的apply(更多详情前点击这里)。

其实这个库结构很简单,但是却实现了很多实用的功能函数,下面在copy一段比较实用函数。

 _.isEmpty = function(obj) {
if (obj == null) return true;
if (_.isArray(obj) || _.isString(obj) || _.isArguments(obj)) return obj.length === 0;
for (var key in obj) if (_.has(obj, key)) return false;
return true;
};
_.isElement = function(obj) {
return !!(obj && obj.nodeType === 1);
};
_.isArray = nativeIsArray || function(obj) {
return toString.call(obj) === '[object Array]';
};
_.isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
_.isNaN = function(obj) {
return _.isNumber(obj) && obj !== +obj;
};
_.isBoolean = function(obj) {
return obj === true || obj === false || toString.call(obj) === '[object Boolean]';
};
_.has = function(obj, key) {
return obj != null && hasOwnProperty.call(obj, key);
};

欢迎加入Javascript前端技术,群号为:85088298

最新文章

  1. 利用Bootstrap快速搭建个人响应式主页(附演示+源码)
  2. 给inpu加背景图,input内容又不能盖着背景图
  3. [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
  4. Tomcat 7 Connector 精读(1)
  5. theano log softmax 4D
  6. android WIFI的一些属性
  7. Javascrpit学习之路一——基础知识
  8. cmd 配置dchp服务器
  9. SENG201 (Software Engineering I) Project
  10. tp5.0 结合 Redis Cache缓存风暴
  11. java-Freemarker TemplateLoader实现模版
  12. centOS 6.5采用python+nginx+uwsgi实现爬金十财经日历
  13. IQC来料检验报表
  14. NGUI 背景图自适应
  15. 【转】使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
  16. javaweb登录界面连接数据库
  17. iOS9新特性-UIStackView
  18. 我用Python远程探查室友的网页浏览记录,他不愧是成年人!
  19. 微商营销实战技巧分享,轻松月入10W
  20. Tensorflow瞎搞

热门文章

  1. get the text value of a selected option.
  2. 说说oracle的 sysdate、trunc函数
  3. 对exp full 和 imp full的认识
  4. Static Class (静态类)
  5. 你真的了解console吗?
  6. asp.net 调用天气所遇到的问题
  7. echarts的使用
  8. laravel观察者模式
  9. jQuery.noConflict()防冲突机制
  10. Yii2——MYSQL操作