阅读underscore源码笔记
来源:互联网 发布:芜湖java招聘 编辑:程序博客网 时间:2024/05/23 15:05
nderscorejs,一个实用的的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
标签的默认事件。例如:
1
2
3
4
5
void 0
void (0)
void
"hello"
void (
new
Date())
//都将返回undefined
为什么使用0,我只想说呵呵,谁让0
最短小可爱呢。
- ECMAScript5中的bind,underscore的实现方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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一段比较实用函数。
1 _.isEmpty = function(obj) { 2 if (obj == null) return true; 3 if (_.isArray(obj) || _.isString(obj) || _.isArguments(obj)) return obj.length === 0; 4 for (var key in obj) if (_.has(obj, key)) return false; 5 return true; 6 }; 7 _.isElement = function(obj) { 8 return !!(obj && obj.nodeType === 1); 9 };10 _.isArray = nativeIsArray || function(obj) {11 return toString.call(obj) === '[object Array]';12 };13 _.isObject = function(obj) {14 var type = typeof obj;15 return type === 'function' || type === 'object' && !!obj;16 };17 _.isNaN = function(obj) {18 return _.isNumber(obj) && obj !== +obj;19 };20 _.isBoolean = function(obj) {21 return obj === true || obj === false || toString.call(obj) === '[object Boolean]';22 };23 _.has = function(obj, key) {24 return obj != null && hasOwnProperty.call(obj, key);25 };
0 0
- 阅读underscore源码笔记
- Underscore 源码学习笔记
- underscore源码学习笔记(1)
- underscore笔记
- underScore源码解析-----Collections
- 源码学习----underscore
- underscore源码经典--收藏
- underscore.js源码分析
- UnderScore 源码解析
- underscore源码解读 -- 目录
- LZ77源码阅读笔记
- Boa 源码阅读笔记
- VNC源码阅读笔记
- osqa源码阅读笔记
- VNC源码阅读笔记
- MPlayer 源码阅读笔记
- PyEmu源码阅读笔记
- JSONModel源码阅读笔记
- 二维码小程序
- faac库编码PCM数据
- Android 将View转化为Bitmap,getDrawingCache为空解决办法
- x264编码-码率控制
- java web项目用cookie记住用户名、密码
- 阅读underscore源码笔记
- 杂事 游泳记事
- 搭建基于注解方式的SSH项目框架
- maven教程三之maven命令
- Difference between enabled and userInteractionEnabled properties
- 企业级搜索应用服务器Solr4.10.4部署开发详解(1)- Solr入门-安装部署
- android 如何解决scrollTo无法执行
- Spring面试题
- iOS录音实践