Giter Site home page Giter Site logo

Comments (17)

hankxdev avatar hankxdev commented on July 29, 2024 5

一直想提醒一下,Github不是BBS,若非有实质内容,诸如“谢谢楼主”,“感谢”这类的信息可否不要发布在issues讨论里面。

from underscore-analysis.

lessfish avatar lessfish commented on July 29, 2024 3

@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.

tudewutong avatar tudewutong commented on July 29, 2024 1

判断Dom元素的方法,如果对象里有nodeType属性呢。。。
_.isElement({nodeType:1})

from underscore-analysis.

jweboy avatar jweboy commented on July 29, 2024

mark~

from underscore-analysis.

EdwardZZZ avatar EdwardZZZ commented on July 29, 2024

_.isNaN() 更新了,用的是native的isNaN方法。

from underscore-analysis.

lessfish avatar lessfish commented on July 29, 2024

@EdwardZZZ 确实,感谢指出~

from underscore-analysis.

FarmanYu avatar FarmanYu commented on July 29, 2024

isNaN有Native的

from underscore-analysis.

lessfish avatar lessfish commented on July 29, 2024

@FarmanYu 对的,edge 版本已经用 native 的函数判断了

from underscore-analysis.

pod4g avatar pod4g commented on July 29, 2024

有2点疑问:

1、isNaN有原生的,直接使用即可,为什么前面还要判断isNumber?这样做是为了排除那些不常见的边界情况?

2、_.isNumber(obj) && obj !== +obj; ,只有_.isNumber(obj)true才会执行第二个判断,执行到第二个判断就说明一定是number类型的,但是为什么又要把obj用+号转一次呢?

from underscore-analysis.

pod4g avatar pod4g commented on July 29, 2024

@hanzichi 原来是这样的。谢谢!underscore变了思路。我受原生isNaN的影响,觉得 不是一个数字 就意味着不是一个数字,例如,undefined确实不是一个数字。。underscore收窄了范围(不进行强制类型转换了)。

再次感谢楼主!

from underscore-analysis.

aleen42 avatar aleen42 commented on July 29, 2024

@hanzichi hey, 我想把你的這些分析, 寫成英文, 並放在自己的筆記中以供學習. 可以嗎? 鏈接: https://aleen42.gitbooks.io/personalwiki/content/Programming/JavaScript/Framework/underscore/type_inference_and_tutorials/type_inference_and_tutorials.html

from underscore-analysis.

lessfish avatar lessfish commented on July 29, 2024

@aleen42 可以,不过希望能指明出处,谢谢~

from underscore-analysis.

lessfish avatar lessfish commented on July 29, 2024

@tudewutong 这确实是个问题 ...

from underscore-analysis.

Baoyx007 avatar Baoyx007 commented on July 29, 2024
_.isElement = function(obj) {
  // 确保 obj 不是 null 
  // 并且 obj.nodeType === 1
  return !!(obj && obj.nodeType === 1);
};

这里为什么要加两个! 呢?

from underscore-analysis.

lessfish avatar lessfish commented on July 29, 2024

@Baoyx007 强制转为布尔值,因为函数返回的是一个布尔值(虽然大对数情况下不加 !! 也问题不大,毕竟会隐式转换)

from underscore-analysis.

shaunzeng avatar shaunzeng commented on July 29, 2024

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.

jingaier avatar jingaier commented on July 29, 2024

var _ = function (obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};
楼主你好,看源码,这段能给解读下吗?为何要这样写?

from underscore-analysis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.