js判断空对象的方法(foreach跳出本次循环)

2022-07-18,,,,

javascript是一种发展迅速的语言。这篇文章,我想展示一些有关如何在javascript中应用函数式编程的示例。

javascript中的函数式编程

即使函数式编程可以极大地改善应用程序的代码,但其原理在开始时可能会有些挑战。由于详细解释所有这些都将花费大量时间,因此我们决定使用两个实际的代码示例来介绍这些概念

1.maybe monad

在第一个示例中,我们找到一种避免验证变量是否为null的方法。假设在我们的应用程序中,我们可以找到具有以下格式的用户:

const someuser = {
 name: 'some_name',
 email: 'some@email.com',
 settings: {
  language: 'sp'
 }
};

有一个功能,可以以用户设置的语言返回欢迎消息。

const allgreetings = {
 'en': '嗨',
 'sp': '你好',
 'fr': '欢迎你'
};
const getgreetingforuser = (user) => {
 //将要执行
}

来看一个遵循命令式模型的“ getgreetingforuser”函​​数的实现:

const getgreetingforuser = (user) => {
 if (!user) {
   return allgreetings.en;
 }
 if (user.settings && user.settings.language) {
   if (allgreetings[user.settings.language]) {
     return allgreetings[user.settings.language]
   } else {
     return allgreetings.en;
   }
 } else {
   return allgreetings.en;
 }
};
console.log(getgreetingforuser(someuser));

如上面所看到的,必须检查用户是否已经存在,是否已设置语言,以及是否已准备好欢迎消息。如果出现问题,我们将以默认语言返回一条消息。

现在,让我们看一下相同的函数,但是这次我们将在其实现中使用函数式编程:

const getgreetingforuser = (user) => {
  return ramdafantasy.maybe(user)
    .map(ramda.path(['settings', 'language']))
    .chain(maybegreeting);
};
const maybegreeting = ramda.curry((greetingslist, userlanguage) => {
  return ramdafantasy.maybe(greetingslist[userlanguage]);
})(allgreetings);
console.log(getgreetingforuser(someuser).getorelse(allgreetings.en));

为了处理可能为null或未定义的情况,我们将使用maybe monad。这使我们可以在对象周围创建包装器,并为空对象分配默认行为。

让我们比较两种解决方案:

//代替验证用户是否为空
if (!user) {
  return allgreetings.en;
}
//我们将用:
ramdafantasy.maybe(user) //我们将用户添加到包装器中
//代替:
 if (user.settings && user.settings.language) {
   if (allgreetings[user.settings.language]) {
//我们将用:
 <usermaybe>.map(ramda.path(['settings', 'language'])) //如果存在数据,映射将会用它
//不是在else中返回默认值:
 return indexurls['en'];
.getorelse(allgreetings。en)
// 指定的默认值。

2 either monad

当我们知道存在空错误时的默认行为时,maybe monad非常有用。

但是,如果我们有一个引发错误的函数,或者我们将各种引发错误的函数链接在一起,并且我们想知道哪个发生了故障,则可以改用either monad。

现在,让我们假设我们要计算产品的价格,同时考虑增值税和可能的折扣。我们已经有了以下代码:


const withtaxes = (tax, price) => {
2
 if (!_.isnumber(price)) {
3
 return new error("price is not numeric");
4
 }
5
 return price + (tax * price);
6
};
7
const withdiscount = (dis, price) => { 
8
  if (!_.isnumber(price)) { 
9
    return new error("price is not numeric"); 
10
  } 
11
  if (price < 5) 
12
    return new error("discounts not available for low-priced items"); 
13
  } 
14
  return price - (price * dis);5
}; 
const iserror = (e) => e && e.name === 'error';
const calculateprice(price, tax, discount) => { 
//将要执行
}

让我们来看一个遵循命令式模型的“ calculateprice”函数的实现:

const calculateprice = (price, tax, discount) => {
  const pricewithtaxes = withtaxes(tax, price);
  if (iserror(pricewithtaxes)) {
    return console.log('error: ' + pricewithtaxes.message);
  }
  const pricewithtaxesanddiscount = withdiscount(discount, pricewithtaxes);
  if (iserror(pricewithtaxesanddiscount)) {
    return console.log('error: ' + pricewithtaxesanddiscount.message);
  }
  console.log('total price: ' + pricewithtaxesanddiscount);
}
//我们计算出价值25的产品(含21%的增值税和10%的折扣)的最终价格。
 calculateprice(25, 0.21, 0.10)

现在,让我们了解如何使用either monad重写此函数。

都有两个构造函数,left和right。我们要实现的是将异常存储到left构造函数,并将正常结果(快乐路径)存储到right构造函数。

首先,将更改已经存在的withtaxes和withdiscount函数,以便在出现错误时它们返回left,在一切正常的情况下返回right:

const withtaxes = ramda.curry((tax, price) => {
  if (!_.isnumber(price)) {
    return ramdafantasy.either.left(new error("price is not numeric"));
  }
  return ramdafantasy.either.right(price + (tax * price)); 
});
const withdiscount = ramda.curry((dis, price) => {
  if (!_.isnumber(price)) {
    return ramdafantasy.either.left(new error("price is not numeric")); 
  }
  if (price < 5) { 
    return ramdafantasy.either.left(new error("discounts not available for low-priced items")); 
  } 
  return ramdafantasy.either.right(price - (price * dis)); 
});

然后,我们为right案例创建一个函数(显示价格),为left案例创建另一个函数(显示错误),然后使用它们创建either monad:

const showprice = (total) => { console.log('price: ' + total) }; 
const showerror = (error) => { console.log('error: ' + error.message); }; 
const eithererrororprice = ramdafantasy.either.either(showerror, showprice);

最后,只需要执行monad来计算最终价格:

//计算出价值25的产品(含21%的增值税和10%的折扣)的最终价格。
 eithererrororprice(
   ramdafantasy.either.right(25)
     .chain(withtaxes(0.21))
     .chain(withdiscount(0.1))
)

结论:javascript中的函数式编程

正如我们所看到的,一旦用maybe和either单子分解了代码,就没有那么复杂了。如果使用得当,它们可以使我们的代码更易于阅读和维护。

唯一的不便是我们需要克服的初始障碍,但这可以通过在网上一些示例并进行一些测试来完成。

《js判断空对象的方法(foreach跳出本次循环).doc》

下载本文的Word格式文档,以方便收藏与打印。