写聪明的Javascript

时间:2017-08-23 11:35:49

标签: javascript

对于我认为的所有专家来说,这是一个非常虚假的问题。

我有一堆if语句(16),我试图找到一种方法来编写48行代码,因为我确信它是可能的。

我在任何地方都读到了if / else if语句是不好的做法。

那么如何巧妙地编写这堆代码呢?感谢

  if (!latitude || typeof latitude == 'undefined') {
    latitude == 'undefined';

  } else if( !longitude || typeof longitude == 'undefined' ) {
    longitude == 'undefined';

  } else if( !name || typeof name == 'undefined' ) {
    name == 'undefined';

  } else if( !adresseNum || typeof adresseNum == 'undefined' ) {
    adresseNum == 'undefined';

  } else if( !adresseVille || typeof adresseVille == 'undefined' ) {
    adresseVille == 'undefined';

  } else if( !adresseDpt || typeof adresseDpt == 'undefined' ) {
    adresseDpt == 'undefined';

  } else if( !adresseRg || typeof adresseRg == 'undefined' ) {
    adresseRg == 'undefined';

  } else if( !adresseFr || typeof adresseFr == 'undefined' ) {
    adresseFr == 'undefined';

  } else if( !adresseCp || typeof adresseCp == 'undefined' ) {
    adresseCp == 'undefined';

  } else if( !telephone || typeof telephone == 'undefined' ) {
    telephone == 'undefined';

  } else if( !horaires || typeof horaires == 'undefined' ) {
    horaires == 'undefined';

  } else if( !note || typeof note == 'undefined' ) {
    note == 'undefined';

  } else if( !reviewFinale || typeof reviewFinale == 'undefined' ) {
    reviewFinale == 'undefined';

  } else if( !website || typeof website == 'undefined' ) {
    website == 'undefined';

  } else if( !types || typeof types == 'undefined' ) {
    types == 'undefined';

  } else {
    console.log('All fields OK');
  }

6 个答案:

答案 0 :(得分:3)

var isOk = [
  latitude, 
  longtitude, 
  name, 
  adresseNum, 
  adresseVille, 
  adresseDpt, 
  adresseRg, 
  adresseFr, 
  adresseCp, 
  telephone, 
  horaires, 
  note, 
  reviewFinale, 
  website, 
  types
].every(function (value) {
  return !!value;
});

if (isOk) {
  console.log('All fields OK');
}

但我认为你最好将所有变量组合成对象或形式

答案 1 :(得分:2)

你可以做一个像这样检查它们的功能

var isDefined = [latitude, longtitude, name, adresseNum, adresseVille, adresseDpt, adresseRg, adresseFr, adresseCp, telephone, horaires, note, reviewFinale, website, types].every(function (value) {return !!value;});

答案 2 :(得分:0)

是由自己定义的每个变量,还是来自另一个对象或类似于var telephone = obj.telephone

如果是这种情况,你可以这样做:

var ok = true;
for (var o in obj) {
    if (!obj[o]) {
        obj[o] == 'undefined';
        ok = false;
    }
}

if (ok) {
    console.log('all fields are ok');
}

答案 3 :(得分:0)

可能有效的选项是在参数destructure中使用默认值。基本上你检查每个变量是否未定义(顺便说一下,如果其中一个是值' false'?)会发生什么。
这是针对这种情况特定的......

function functionWithSomeLogic({
    latitude = 'undefined',
    longitude = 'undefined',
    name = 'undefined',
    adresseNum = 'undefined'
    // ... and the rest of the variables
}) {
    // Do whatever here, all the arguments are initialized or have the string value of 'undefined'
} 
顺便说一句,如果不是设置默认值而是想抛出异常

const throwArg = argName => throw new Error(`Argument missing: ${argName}`);

function functionWithSomeLogic({
    latitude = throwArg('latitude'),
    longitude = throwArg('longitude'),
    name = throwArg('name'),
    adresseNum = throwArg('adresseNum')
    // ... and the rest of the variables
}) {
    // Do whatever here, all the arguments are initialized, if one of them
    // had been undefined, an appropriate exception will be thrown before this body
} 

答案 4 :(得分:0)

一个简单的es6函数就可以了。正如@Andrey指出的那样。

const areDefined = (...args) => args.every(elm => !!elm); if(areDefined(latitude, longitude, name/*, ...*/)) { console.log('All fields OK'); }

答案 5 :(得分:0)

使用ES6,您可以使用包含所有变量作为键和值对的对象,然后使用Array#every进行迭代并分配假值undefined并使用if statement检查结果。

var data = { latitude, longitude, name, adresseNum, adresseVille, adresseDpt, adresseRg, adresseFr, adresseCp, telephone, horaires, note, reviewFinale, website, types };

if (Object.keys(data).every(k => data[k] || (data[k] = 'undefined', false))) {
    console.log('All fields OK');
}