Comments (17)
一直想提醒一下,Github不是BBS,若非有实质内容,诸如“谢谢楼主”,“感谢”这类的信息可否不要发布在issues讨论里面。
from underscore-analysis.
@pod4g 好问题。
首先我们明确下 underscore 中 _.isNaN 方法的作用,引用文档 http://underscorejs.org/#isNaN:
Note: this is not the same as the native isNaN function, which will also return true for many other not-number values, such as
undefined
.
也就是说,_.isNaN 如果要返回 true,必须传入一个 Number 类型。
isNaN(undefined);
=> true
_.isNaN(undefined)
=> false
这就是为什么要判断 isNumber 的原因。事实上可以看看 edge 版本的源代码:
// Is the given value `NaN`?
_.isNaN = function(obj) {
return _.isNumber(obj) && isNaN(obj);
};
这就非常清晰了,需要满足 isNumber 和 isNaN 两个条件。
话说回来,满足 isNumber 后,为什么要用 + 号转一次呢?
var a = new Number(NaN);
a !== a;
=> false
a !== +a;
=> true
很显然,我们的变量 a,调用 _.isNaN,希望返回的是 true。
事实上,_.isNaN 可能和 Number.isNaN 更接近一点。
from underscore-analysis.
判断Dom元素的方法,如果对象里有nodeType属性呢。。。
_.isElement({nodeType:1})
from underscore-analysis.
mark~
from underscore-analysis.
_.isNaN() 更新了,用的是native的isNaN方法。
from underscore-analysis.
@EdwardZZZ 确实,感谢指出~
from underscore-analysis.
isNaN有Native的
from underscore-analysis.
@FarmanYu 对的,edge 版本已经用 native 的函数判断了
from underscore-analysis.
有2点疑问:
1、isNaN
有原生的,直接使用即可,为什么前面还要判断isNumber
?这样做是为了排除那些不常见的边界情况?
2、_.isNumber(obj) && obj !== +obj;
,只有_.isNumber(obj)
为true
才会执行第二个判断,执行到第二个判断就说明一定是number
类型的,但是为什么又要把obj用+号转一次呢?
from underscore-analysis.
@hanzichi 原来是这样的。谢谢!underscore
变了思路。我受原生isNaN
的影响,觉得 不是一个数字
就意味着不是一个数字
,例如,undefined
确实不是一个数字。。underscore
收窄了范围(不进行强制类型转换了)。
再次感谢楼主!
from underscore-analysis.
@hanzichi hey, 我想把你的這些分析, 寫成英文, 並放在自己的筆記中以供學習. 可以嗎? 鏈接: https://aleen42.gitbooks.io/personalwiki/content/Programming/JavaScript/Framework/underscore/type_inference_and_tutorials/type_inference_and_tutorials.html
from underscore-analysis.
@aleen42 可以,不过希望能指明出处,谢谢~
from underscore-analysis.
@tudewutong 这确实是个问题 ...
from underscore-analysis.
_.isElement = function(obj) {
// 确保 obj 不是 null
// 并且 obj.nodeType === 1
return !!(obj && obj.nodeType === 1);
};
这里为什么要加两个! 呢?
from underscore-analysis.
@Baoyx007 强制转为布尔值,因为函数返回的是一个布尔值(虽然大对数情况下不加 !! 也问题不大,毕竟会隐式转换)
from underscore-analysis.
could you also explain why in _.where method, _.isMatch is passed in that returns a boolean. How the _.filter method functions when the predicate is a boolean? thanks!!
from underscore-analysis.
var _ = function (obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};
楼主你好,看源码,这段能给解读下吗?为何要这样写?
from underscore-analysis.
Related Issues (20)
- JavaScript 数组乱序 HOT 13
- 浅谈 underscore 内部方法 group 的设计原理 HOT 3
- 带注释的源码中var _ = function (obj) {....}中有个疑问 HOT 4
- 关于 bind 你可能需要了解的知识点以及使用场景 HOT 2
- bind 方法的兼容实现 HOT 1
- JavaScript 函数节流和函数去抖应用场景辨析 HOT 17
- underscore 函数去抖的实现 HOT 20
- underscore 函数节流的实现 HOT 8
- 从斐波那契数列求值优化谈 _.memoize 方法 HOT 6
- Function Functions 相关源码拾遗
- 浅谈 Web 中前后端模板引擎的使用 HOT 19
- Underscore _.template 方法使用详解 HOT 3
- Underscore 整体架构浅析 HOT 15
- 源码解读这半年 HOT 20
- 疑问:模块导出中,向后兼容老的require() API HOT 1
- 源码
- 打扰一下,有一个问题想咨询你一下 HOT 9
- 你好请教个问题 HOT 1
- 你好...在谷歌浏览器中运行`Object.prototype.toString().call(abb)`会报错 //var abb = true HOT 2
- 關於underscore源碼中提到了25個扩展方法之疑問? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from underscore-analysis.