javascript - 如何在javascript中的语句之前理解下划线
问题描述
_
此示例代码中的含义是什么:
if (_(abc.content).has("abc")){
console.log("abc found");
}
很多人说“_”表示私有成员,但是如果abc
orcontent
是私有成员,我们不应该使用_abc.content
orabc._content
吗?
谢谢
解决方案
要使其有效,_
必须引用一个函数。也许脚本正在使用underscore
,在这种情况下,如果对象的键为,则_(abc.content).has("abc")
返回布尔值,否则:true
abc.content
abc
false
const abc = { content: { key1: 'foo', abc: 'bar' } };
if (_(abc.content).has("abc")){
console.log("abc found");
}
console.log(_(abc.content).has("keyThatDoesNotExist"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
它可能与私有属性无关,因为_
它是一个独立的函数。
使用的库也可能是 lodash:
const abc = { content: { key1: 'foo', abc: 'bar' } };
if (_(abc.content).has("abc")){
console.log("abc found");
}
console.log(_(abc.content).has("keyThatDoesNotExist"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.min.js"></script>
但可以肯定的是,您必须检查它_
,console.log
或者查看它的定义位置,以获得一些想法。
推荐阅读
- reactjs - 错误:使用 Redux connect() 时,路由“HomeScreen”的组件必须是 React 组件
- apache-spark - Spark 作业失败:无法创建本地 tmp 目录
- c++ - 如何在 C++ 中使用频率表计算模式
- linux - Linux shell 脚本中的条件
- python - 如何添加相同键的元组并排序
- jquery - 热门在多个元素上使用 prependTo
- sql - Oracle SQL 将字符串转换为数字,但有例外将文本视为 0
- php - 为什么我在不同的在线编译器上得到不同的输出?
- android - 我确定 Android 服务何时启动?
- amazon-web-services - 如何使用 RDS 作为存储来组织 Spring 应用程序的 e2e 测试