javascript - 你能分配一些东西来比较吗?
问题描述
在下面的示例中,分配了一些东西来返回。我想知道写这篇文章的规则。
function confirmEnding(str, target) {
return str.slice(str.length - target.length) === target;
}
confirmEnding("He has to give me a new name", "name");
是否与以下逻辑相同:如果 === 之前的内容等于返回 true 之后的内容,如果不返回 false?
解决方案
是的。相同的函数可以用许多不同的方式编写。它实际上并不称为作业。 a = b
是在a
赋值完成后计算为新值的赋值。但是a == b
(or a === b
) 是一个相等(后者,strict
相等)测试,其计算结果为true
or false
。
function confirmEnding(str, target) {
return str.slice(str.length - target.length) === target;
}
function confirmEnding(str, target) {
if (str.slice(str.length - target.length) === target) {
return true;
} else {
return false;
}
}
function confirmEnding(str, target) {
return str.slice(str.length - target.length) === target
? true
: false
}
const confirmEnding = (str, target) => str.slice(str.length - target.length) === target;
推荐阅读
- amazon-web-services - 向在 AWS Cognito 控制台中创建的用户添加自定义属性
- r - 基于类似重复出现的条件列创建
- r - 使用 R 匹配数据框中的复杂字符串名称
- linux - newlib:编译器在构建 32 位兼容性时会感到困惑
- javascript - 如何过滤两个深度数组
- c# - 如何解决 VSIX -Visual Studio 扩展项目中 Azure.Core.dll 的引用?
- c# - 如何在 c# 中获取总小时数,可能超过 24 小时
- python - 我如何按周和年分组,并根据这一点,将值分配给最初将“N”作为所有值的第三列?
- google-apps-script - 无法在谷歌应用脚本中使用 DocumentApp.openById() 打开文档
- c# - 查找两个锚点之间的元素包括具有 XPath 的锚点的父级