首页 > 解决方案 > 你能分配一些东西来比较吗?

问题描述

在下面的示例中,分配了一些东西来返回。我想知道写这篇文章的规则。

function confirmEnding(str, target) {

  return str.slice(str.length - target.length) === target;
}

confirmEnding("He has to give me a new name", "name");

是否与以下逻辑相同:如果 === 之前的内容等于返回 true 之后的内容,如果不返回 false?

标签: javascriptfunction

解决方案


是的。相同的函数可以用许多不同的方式编写。它实际上并不称为作业。 a = b是在a赋值完成后计算为新值的赋值。但是a == b(or a === b) 是一个相等(后者,strict相等)测试,其计算结果为trueor 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;

推荐阅读