javascript - _.isUndefined 实现

标签 javascript underscore.js

为什么underscore.js的isUndefined是这样定义的?

_.isUndefined = function(obj) { 返回对象 === void 0; };

为什么这行不通?

typeof obj === 'undefined'

最佳答案

好的,首先 typeof obj === 'undefined' 比较慢,因为您可以轻松验证。

那么问题是为什么要进行比较

obj === void 0 

对比

obj === undefined

让我们看看:

void 0; 返回一元运算符 void 的结果,它将始终返回 undefined(即 void 1 没有区别)

undefined 指向全局变量undefined

一般情况下两者是一样的。我认为 void 0 是首选,因为可以用局部变量 undefined 隐藏 undefined :) 这很愚蠢,但确实发生了。

关于javascript - _.isUndefined 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13463955/

相关文章:

javascript - 在回调函数中调用类方法

javascript - 停止提交空输入值

javascript - 自定义 Underscore.js 的 range()

javascript - 自适应 FitText : is there a better way to do it?

javascript - 如何将某些对象键分离到它们自己的数组中?

javascript - React Navigation - 如何在 axios 实例中访问 this.props.navigation

javascript - 下划线JS : Is there a way to iterate a JSON structure recursively?

javascript - 单击按钮无法在 laravel ajax 中下载文件

javascript - _Filrer 使用多个条件过滤 JSON

javascript - 数组的数组到对象