简体   繁体   中英

Javascript function not getting called? Scope issue?

I am trying to perform validation on a json object. But it seems that the method ispoQuantityInvalid method is not getting called at all.

Fiddle

selectedRows = [{
    "itemNumber": "",
    "lineNumber": 1,
    "description": "PUMP,COMPLETE, P/N E12LYTFS-137",
    "uom": "",
    "poQuantity": "1",
    "recievedQuantity": "3",
    "isMatched": false,
    "p2PLineItemId": "168512",
    "quantityUnitPrice": "1",
    "buyerItemNumber": null
}];    


 ispoQuantityInvalid = () => (element, index, array) => {
 alert() //not getting called. 
    return this.isEmpty(element.poQuantity);
  }
console.log(this.selectedRows)

if (this.selectedRows.some(this.ispoQuantityInvalid)) {
     console.log('po qty is null or empty') //always gets called.
      return;
    }    

  isEmpty = (value) => {     
    return (value == null || value.length === 0 || value === '' || value == 0);
  }
  1. Even though poQuantity is not null or empty or 0, it is failing validation.
  2. It seems ispoQuantityInvalid method is not getting called at all. I don't see alert.

What am I missing here? It seems to be a scope issue. But I am using arrow function that is supposed to not cause this issue right?

change

 ispoQuantityInvalid = () => (element, index, array) => {
 alert() //not getting called. 
    return this.isEmpty(element.poQuantity);
  }

to

 ispoQuantityInvalid = (element, index, arra) => {
 alert() //not getting called. 
    return this.isEmpty(element.poQuantity);
  }

The first one is a function that returns the function you want to call. Therefore the inner function is only available but never called. The if-statement then looks like if( function(e,i,a) {} ) which is not false, so the if statement body gets executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM