JSFiddle - React, Tailwind, and code Playground

by rpflorence

JavaScript

var model = {
  foo: 'bar',
  validate: function (){
    if (this.foo !== 'bar'){
      this.errors = ' ERRORS!!! ';
      return false;
    }
    return true;
  }
};

var x = Object.create(model),
    y = Object.create(model);

y.foo = 'baz';

// y always fails and returns false
if (x.validate(), y.validate()){
  // won't make it here because y.validate returns false
} else {
  console.log(x.errors + y.errors);
}

if (y.validate(), x.validate()){
  // shouldn't make it here because y fails
  // but it makes it here because x passes
  console.log('All is well in zion');
} else {
  // won't make it here even though y failed!!
  // don't know if that's what you want
}


// what you're after, maybe there's a fancier way, iunno
if (x.validate() + y.validate() > 1){
  // it's 1
} else {
  console.log(y.errors + x.errors);
}

y.foo = 'bar'; // will validate now
if (x.validate() + y.validate() > 1){
  console.log('success!')
} else {
  // won't make it
}